我想找一个特殊的单词,在字符串中使用 preg_match 并回显它后面的100个字符。
为此,我需要知道preg_match()
找到的单词的位置。
也许你会建议我使用strpos()
找到它的位置,但strpos()
的问题是我的字符串中有两个单词“smart”和“art”并且“聪明” “在”艺术“之前,strpos发现”聪明“,而我希望它能找到”艺术“。这就是为什么我决定使用preg_match()
。
以下代码是我写的:
<?php pattern = "/\b' .$data[title]. '\b/i" ;
$article= $data['description'];
preg_match($pattern, $article, $matches); echo '. $matches .' ;
?>
例如:
<?php $pattern = "/\b' .art. '\b/i" ;
$article= "I didn’t think parents do not like that either…but son is so smart.who is studying art therapy";
preg_match($pattern, $article, $matches);
echo '. $art .' ;
?>
答案 0 :(得分:0)
试试这个直接获得100个字符:
JOIN
输出:
<?php
$pattern = "/\bart\b(.{100})/i";
$article= "I didn’t think parents do not like that either…but son is so smart.who is studying art therapy I didn’t think parents do not like that either…but son is so smart I didn’t think parents do not like that either…but son is so smart";
preg_match($pattern, $article, $matches);
print_r($matches);
echo 'Length: '+strlen($matches[1])
?>