如何从php字符串中修剪整个单词
就像我不想要"foo"
,"bar"
,"mouse"
单词在左边,"killed"
在字符串的末尾。
所以"fo awesome illed"
not
会被修剪
但是"foo awesome killed"
=> " awesome "
"killed awesome foo"
没有修剪(从右边删除,从左边删除)
当我使用ltrim($str, "foo")
时,它将修剪"foo"
- "f"
或"o"
中的任何字母
发布于 2013-09-12 00:31:53
使用preg_replace()
将字符串替换为空字符串。
$str = preg_replace('/^(foo|bar|mouse)|killed$/', '', $str);
到regular-expressions.info了解有关regexp的更多信息。
发布于 2022-04-12 23:51:42
如果您不需要高级regex特性,str_replace($replaceme, $withme, $here)
工作得更快(也更容易)。如果您知道要查找的确切单词($replaceme),并且希望删除它,可以在$withme参数中使用"“(空字符串)。
发布于 2013-09-12 00:35:26
使用preg_match函数并根据您的要求创建您自己的模式。
<?php
$subject = "abcdef";
$pattern = '/^foo$killed/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
https://stackoverflow.com/questions/18759172
复制相似问题