PHP正则表达式来精确获取我想要的字符串
我有一个代码用于嵌入iframe的链接PHP正则表达式来精确获取我想要的字符串,php,regex,iframe,Php,Regex,Iframe,我有一个代码用于嵌入iframe的链接 $post_contetn = explode('htt',$content); $content_with_link = $post_contetn[0]; $link = 'htt'.$post_contetn[1]; 但问题是,如果我写 http://www.espn.com was great 然后它链接“很棒”是$link的一部分 如何更改(可能使用regex)以仅包含实际url ====== 如果我采纳暹罗的答案,应该是吗
$post_contetn = explode('htt',$content);
$content_with_link = $post_contetn[0];
$link = 'htt'.$post_contetn[1];
但问题是,如果我写
http://www.espn.com was great
然后它链接“很棒”是$link的一部分
如何更改(可能使用regex)以仅包含实际url
======
如果我采纳暹罗的答案,应该是吗
$regex = '/https?:\/\/.*?(?=\s)/';
$post_contetn = preg_match($regex, $content, $linkarray);
$content_with_link = $post_contetn[0];
$link = $linkarray[0]
echo $content_with_link;
然后我编辑到
preg_match($regex, $content, $post_contetn);
$content_with_link = $post_contetn[0];
$link = $post_contetn[0]
echo $content_with_link;
但是错误仍然发生在echo line上。尝试使用以下正则表达式:
请参见
PHP
<?php
$content = 'http://www.espn.com was great';
$regex = '/(?:https?:\/\/\S+)?\S+\.\S+\.?\S+/';
preg_match($regex, $content, $post_contetn);
$link = $post_contetn[0];
echo $link;
?>
@user42459不能将preg\u match()
赋值给变量。只需将$linkarray
替换为$post\u contetn
,您的$content
的格式是什么?@user42459那么您的其他内容可能有问题。代码运行得非常好。看@user42459ya,这很奇怪,我更新了答案。@user42459没问题,你可以随时提问。无论如何,我进一步更新了答案。现在它应该可以正常工作了。没有一个php对regex没有不同的规则。@user42459没有,它没有显示整个asdf espn.com
,它只显示espn.com
请参见
<?php
$content = 'http://www.espn.com was great';
$regex = '/(?:https?:\/\/\S+)?\S+\.\S+\.?\S+/';
preg_match($regex, $content, $post_contetn);
$link = $post_contetn[0];
echo $link;
?>