PHP 和正则表达式(如果找到单词)

发布于 2024-10-31 19:03:36 字数 186 浏览 2 评论 0原文

我希望仅在字符串中找到某个单词时才执行一条字符带,因此如果单词编号与正则表达式匹配,它将执行一条带所有实际数字 0-9 的操作,或者 preg 将它们替换为空,顺便说一句数字总是用“”括起来。将这两个功能组合在一起的最佳方法是什么?一个例子是,如果数据是人,数字很有趣! “123ABC”它会返回伙计,数字很有趣! “ABC” 如果数字不存在,它们将被忽略。

I'm looking to only perform a strip of characters if a certain word is found in a string, so if the word numbers is matched with regex it will perform a strip all actual numbers 0-9 or preg replace them to nothing, BTW The numbers will always be wrapped in "". What would be the best way to put these two functions together? An example would be if the data was Man, Numbers are fun! "123ABC" it would return Man, Numbers are fun! "ABC" If numbers isn't present they are ignored.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回忆追雨的时光 2024-11-07 19:03:36

我觉得这里的一些答案过于复杂。也许只是我,但这应该是您所需要的:

if (stripos($str, 'numbers') !== false) {
    $str = preg_replace('/\d/', '', $str);
}

编辑:如果您只想要引号内的数字,您也许可以使用正则表达式来完成,但我肯定会这样做是这样的:

if (stripos($str, 'numbers') !== false) {
    $arr = explode('"', $str);

    for ($i = 1; $i < count($arr); $i += 2) {
        $arr[$i] = preg_replace('/\d/', '', $arr[$i]);
    }

    $str = implode('"', $arr);
}

I feel like some of the answers here are overcomplicated. Maybe it's just me, but this should be all you need:

if (stripos($str, 'numbers') !== false) {
    $str = preg_replace('/\d/', '', $str);
}

EDIT: If you only want numbers that are inside quotation marks, you might be able to do it with a regex, but I'd definitely do it this way:

if (stripos($str, 'numbers') !== false) {
    $arr = explode('"', $str);

    for ($i = 1; $i < count($arr); $i += 2) {
        $arr[$i] = preg_replace('/\d/', '', $arr[$i]);
    }

    $str = implode('"', $arr);
}
断肠人 2024-11-07 19:03:36

如果我正确理解这个问题,也许是这样的:

if (strpos($string, "numbers") !== false) {
    $string = preg_replace('/"\d+"/', '', $string);
}

If I understand the question correctly, maybe something like:

if (strpos($string, "numbers") !== false) {
    $string = preg_replace('/"\d+"/', '', $string);
}
小梨窩很甜 2024-11-07 19:03:36

像这样的东西应该适合你:

$str = 'Man, Numbers are fun! "123ABC"';
var_dump(preg_replace_callback("(.*\bNumbers\b.*)",
create_function(
'$matches',
'return preg_replace("/(\"[^\d]*)\d+(.*\")/", "$1$2", $matches[0]);'
),
$str));

OUTPUT:
string(27) "Man, Numbers are fun! "ABC""

\bNumbers\b 将确保将单词 Numbers 与单词边界匹配,以便 xyzNumbersNumbersxyz 是 <强>不匹配。

Something like this should work for you:

$str = 'Man, Numbers are fun! "123ABC"';
var_dump(preg_replace_callback("(.*\bNumbers\b.*)",
create_function(
'$matches',
'return preg_replace("/(\"[^\d]*)\d+(.*\")/", "$1$2", $matches[0]);'
),
$str));

OUTPUT:
string(27) "Man, Numbers are fun! "ABC""

\bNumbers\b will make sure to match word Numbers with word boundaries so that xyzNumbers and Numbersxyz are NOT matched.

漫漫岁月 2024-11-07 19:03:36

您可以首先使用 strpos 函数来检查字符串。

类似于

if(strpos($mystr,"Some value")!==false)
{
/*preg_replace在这里。*/
}

You could use the strpos function to check for the string in the first place.

Something like

if(strpos($mystr,"Some value")!==false)
{
/*preg_replace here.*/
}

只为一人 2024-11-07 19:03:36

@Ryan Cooper:尝试—​​—

$input = 'Man, Numbers are fun! "123ABC"';

echo stripnums("Numbers", $input);

function stripnums($needle, $haystack)
{
    if (stripos($haystack, $needle) !== 0)
    {
        return preg_replace('/[0-9]/', '', $haystack);
    }
}

@Ryan Cooper: Try --

$input = 'Man, Numbers are fun! "123ABC"';

echo stripnums("Numbers", $input);

function stripnums($needle, $haystack)
{
    if (stripos($haystack, $needle) !== 0)
    {
        return preg_replace('/[0-9]/', '', $haystack);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文