在特定字符后的PHP字符串中搜索空格

时间:2015-11-26 21:10:48

标签: php string substring

这是我的字符串:

$string = '@ somebody and some other stuff';

如何检测@字符后面的空格?

如果我找到了匹配,我想用原始字符串做一些事情。

$string2 = '@ ';

2 个答案:

答案 0 :(得分:2)

如果我理解正确,您想找到@并删除空格?

$string = str_replace("@ ", "@", $string);

修改 你想要这个PHP source

<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?>

答案 1 :(得分:0)

$string = preg_replace('/@ (.*)/', '@ ', $string);