[英]PHP Regex: how to remove some of the words in the link
这是我的第一篇文章。 我已经有几天尝试过了,但没有成功。
案例:我想从此链接中删除链接中的一些单词“ http://www.amazon.com/Mindfulness-Be-mindful-Live-moment/dp/0857084445/ref=sr_1_1?s=books&ie=UTF8&qid= 1443534574&sr = 1-1&keywords = Mindfulness + Be + Mindful + Live + In + The + Moment “
这是我的代码
<?php
$s = 'http://www.amazon.com/Mindfulness-Be-mindful-Live-moment/dp/0857084445/ref=sr_1_1?s=books&ie=UTF8&qid=1443534574&sr=1-1&keywords=Mindfulness+Be+Mindful+Live+In+The+Moment';
preg_match("/http:/(.*)//", $s, $results);
echo $results[0];
?>
对于substr函数,否,因为链接是动态的。 我想使用php regex保留“ 0857084445”。 您的解决方案确实对我有所帮助。 谢谢
您应该使用此正则表达式#/(\\d+)/#i
匹配2 /之间的每个数字。
不仅您的regexp错误,而且reults的输出也是如此。 由于$results[0]
包含匹配的字符串。 $results[1]
第一个按captureted的组,依此类推。
这是更正的代码
<?php
$s = 'http://www.amazon.com/Mindfulness-Be-mindful-Live-moment/dp/0857084445/ref=sr_1_1?s=books&ie=UTF8&qid=1443534574&sr=1-1&keywords=Mindfulness+Be+Mindful+Live+In+The+Moment';
preg_match('#/(\d+)/#', $s, $results);
echo $results[1];
?>
如果您的问题仅与显示目的有关,那么这样做就容易得多,因为不需要正则表达式。
<?php
$s = 'http://www.amazon.com/Mindfulness-Be-mindful-Live-moment/dp/0857084445/ref=sr_1_1?s=books&ie=UTF8&qid=1443534574&sr=1-1&keywords=Mindfulness+Be+Mindful+Live+In+The+Moment';
?> <a href="<?php echo $s?>"> Whatever you want here</a>
编辑:如果我有50个代表,我会问它是否仅在评论部分显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.