Php 查找文本中字符串的第一个匹配项并返回原始文本的示例
我有一个包含所有特殊字符的文本,我需要找到字符串的第一个匹配项,然后返回10个字符+第一个匹配项+10个字符。换句话说,第一个匹配的文本样本位于中间 例如:Php 查找文本中字符串的第一个匹配项并返回原始文本的示例,php,regex,string,replace,substr,Php,Regex,String,Replace,Substr,我有一个包含所有特殊字符的文本,我需要找到字符串的第一个匹配项,然后返回10个字符+第一个匹配项+10个字符。换句话说,第一个匹配的文本样本位于中间 例如: $text = "+!This is a text with some % special chars/text and ( for a string query to match. Then !! mor&&&e and so on...chars/text."; $stringToFind = "hars/t";
$text = "+!This is a text with some % special chars/text and ( for a string query to match. Then !! mor&&&e and so on...chars/text.";
$stringToFind = "hars/t";
它应返回:
$grabtext = "% special chars/text and ( for";
我举了一个字符串的例子,发现它不是一个完整的单词,有一个特殊的字符。这可以用正则表达式来完成。
是任何字符(不包括新行),{}
是字符限制或范围。在本例中,我们将允许任意字符的10,{10}
~(.{10}hars/t.{10})~
正则表达式演示:
PHP演示:
用法:
$text = "+!This is a text with some % special chars/text and ( for a string query to match. Then !! mor&&&e and so on...chars/text.";
$stringToFind = "hars/t";
preg_match('~.{10}' . preg_quote($stringToFind, '~') . '.{10}~', $text, $match);
print_r($match);
请注意,special c
是10个字符,hars/text
是您的匹配项,ext和(
是额外的10个字符。您找到的字符串或描述已关闭
返回10个字符+第一次匹配+10个字符
更新,最多允许使用10个字符:
$text = "+!This is a text with some % special chars/text and ( for a string query to match. Then !! mor&&&e and so on...chars/text.";
$stringToFind = "hars/t";
preg_match('~.{0,10}' . preg_quote($stringToFind, '~') . '.{0,10}~', $text, $match);
print_r($match);
演示:这可以用正则表达式来完成。a
是任何字符(不包括新行),{}
是字符限制或范围。在这种情况下,我们将允许任何字符中的10个,{10}
~(.{10}hars/t.{10})~
正则表达式演示:
PHP演示:
用法:
$text = "+!This is a text with some % special chars/text and ( for a string query to match. Then !! mor&&&e and so on...chars/text.";
$stringToFind = "hars/t";
preg_match('~.{10}' . preg_quote($stringToFind, '~') . '.{10}~', $text, $match);
print_r($match);
请注意,special c
是10个字符,hars/text
是您的匹配项,ext和(
是额外的10个字符。您找到的字符串或描述已关闭
返回10个字符+第一次匹配+10个字符
更新,最多允许使用10个字符:
$text = "+!This is a text with some % special chars/text and ( for a string query to match. Then !! mor&&&e and so on...chars/text.";
$stringToFind = "hars/t";
preg_match('~.{0,10}' . preg_quote($stringToFind, '~') . '.{0,10}~', $text, $match);
print_r($match);
演示:
strpos
-查找子字符串在字符串中第一次出现的位置
因此,如果我们使用该函数来定位子字符串的偏移量,我们可以很容易地使用类似于从该偏移量获得10个字符+/-的子字符串的东西
function grabText($string, $searchString) {
if (($x = strpos($string, $searchString)) === false) {
return; // no match found
}
$y = strlen($searchString) + 20;
$x = max(0, $x - 10);
return substr($string, $x, $y);
}
$text = "+!This is a text with some % special chars/text and ( for a string query to match. Then !! mor&&&e and so on...chars/text.";
$stringToFind = "hars/t";
echo grabText($text, $stringToFind); // special chars/text and (
strpos
-查找子字符串在字符串中第一次出现的位置
因此,如果我们使用该函数来定位子字符串的偏移量,我们可以很容易地使用类似于从该偏移量获得10个字符+/-的子字符串的东西
function grabText($string, $searchString) {
if (($x = strpos($string, $searchString)) === false) {
return; // no match found
}
$y = strlen($searchString) + 20;
$x = max(0, $x - 10);
return substr($string, $x, $y);
}
$text = "+!This is a text with some % special chars/text and ( for a string query to match. Then !! mor&&&e and so on...chars/text.";
$stringToFind = "hars/t";
echo grabText($text, $stringToFind); // special chars/text and (
您是否尝试过使用str_pos()?它提供了字符串中第一个匹配字符的位置。然后您可以猜测要打印的第一个字符的位置,以及最后一个字符的位置……您尝试过使用str_pos()吗它给了你第一个匹配字符串的位置。然后你可以猜出你想要打印的第一个字符的位置,最后一个…谢谢克里斯,如果我的String Fuffing是在开始或结束的时候,你有什么建议吗?(EX:这个)。如果字符串在中间,或者至少每边有足够的10个字符,那么效果就很好。(我可以在原始文本的开头和结尾添加10个额外空格,然后修剪最终结果,但似乎不太美观)你认为呢?你可以修改范围,使
10
成为可选长度。(.{0,10}hars/t.{0,10})
将允许在匹配之前和/或之后使用0-10个字符。演示2:您可以在此处阅读更多内容,请参阅限制重复
。感谢Chris和演示。我真的应该阅读更多关于正则表达式的内容,但我的php非常有限。谢谢Chris…如果我的stringToFind在开始或结束(例如:!This),您有什么建议吗如果字符串在中间,或者至少每边都有10个字符,则效果很好。(我可以在开始和结束时在原始文本中添加10个额外的空间,然后修剪最终结果,但是看起来好像不太漂亮)你怎么想?你可以修改范围,使<代码> 10 可选长度。<代码>({0,10}HARS/T)。{0,10})将允许在匹配前和/或匹配后使用0-10个字符。演示2:您可以在此处阅读更多内容,请参阅限制重复
。感谢Chris和演示。我确实应该阅读更多关于正则表达式的内容,但我的php非常有限。感谢谢里夫,这是一个非常好的函数,它确实满足了我的需要!感谢谢里夫,这是一个非常好的函数y做了我需要的!