2016-07-30 23 views
0

我想使用的preg_match的preg_match动态文本和回声

$desc = 'this is some text big <a href="//example.com/dynamicID">//example.com/dynamicID</a><br> and some more text here'; 

$desiredtext = preg_match("//example.com/.*?", $desiredtext, $desc); 

echo $desiredtext; //this should return me only //example.com/dynamicID 

如何解决这个问题,以得到一个字符串动态文本?

回答

1

尝试以下方法:(!更新)

if(preg_match("/(?<=href=")([^"]*)/", $desc,$matches)) $desiredtext=$matches[1]; 

它采用了回顾后组搜索href="。然后捕获组([^"]*)将抓取任何东西,直到下一个"。调整正则表达式以适应您的要求取决于您。

回应您的评论:

延长第一检索组()包括“example.com”,然后从matches[1]而不是matches[0]得到的结果:

preg_match('/(example.com[^&]*)/',$desc,$matches); 
$part = $matches[1]; 
echo $part; 
+0

感谢您的回复,但问题是在文本中有多个href如何正确地找到我们需要的?有时在第二个href和第七个href的一些时候所需的文本。 – KenDev

+0

你的第二个和第三个参数是相反的,并且你在存储1,0或者'FALSE',你想要存储匹配 – sidyll

+0

你能准确地描述“我们需要什么”吗?你正在寻找一个特定的网址或死于它必须在特定的''? – cars10m