Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用正则表达式从链接中提取单引号和双引号URL_Php_Regex_Url_Href_Quoting - Fatal编程技术网

Php 使用正则表达式从链接中提取单引号和双引号URL

Php 使用正则表达式从链接中提取单引号和双引号URL,php,regex,url,href,quoting,Php,Regex,Url,Href,Quoting,我需要使用正则表达式为PHP脚本提取Twitter ID。只要URL是用双引号编码的,它就工作得很好 <a href='http://www.twitter.com/singlequotes'>Twitter Single Quotes</a> <a href="http://www.twitter.com/doublequotes">Twitter Double Quotes</a> // regular expression /<a [

我需要使用正则表达式为PHP脚本提取Twitter ID。只要URL是用双引号编码的,它就工作得很好

<a href='http://www.twitter.com/singlequotes'>Twitter Single Quotes</a>
<a href="http://www.twitter.com/doublequotes">Twitter Double Quotes</a>

// regular expression
/<a [^>]*\bhref\s*=\s*"\K[^"]*twitter.com[^"]*/

//正则表达式

/

这是你能走的最快的速度。不需要捕获组

href=['”]\K[^']+

href=
之后查找单引号或双引号,然后匹配不是单引号或双引号的所有内容。这是尽可能简单的

p、 s.如果您关心
=
附近的空格,请使用:

href*=*['”]\K[^']+

PHP实现():

$in='1!'
';
$companies=['twitter','facebook'];
$out=preg\u match\u all('/href*=*[\'”]\Khttps?:\/\/(?:www\.)?(?:'.内爆('.\',$companys.))\.com[^\'“]+/',$in,$out)?$out[0]:[];
var_出口(美元);

但是你还必须在
[^”]
中添加顶点,这很好,但是在我的
preg\u match\u all中使用这个表达式(“/]*\bhref\s*=\s*['”]\K[^']*twitter.com[^']*/g“,$result,$results)导致
分析错误:语法错误,意外的''']\K[^''(T_常量\u封装的\u字符串)在…
非常感谢您的支持。但我的问题是,我需要在我的模式中定义特定的URL。在这种情况下,twitter.com可以是facebok.com…在这些情况下,模式会是什么样子?谢谢。但是
preg\u match\u all(“/href=[”]\Khttps?:\/\/www\。(?:twitter | facebook)\.com[^']+/g”,$result,$results);
也会导致我的PHP代码出现解析错误。:(我不知道如何解决这个问题。Regex工作得很好。太棒了,谢谢!我尝试过,但没有找到解决这个问题的方法:我如何在
href=“
anywhere.com
之间通配符,因为我没有得到
href=”的任何结果https://twitter.com...
例如…谢谢!这太棒了!非常感谢!只是为了确保我以后不会打扰你:如果我想排除包含特定字符(如
)的URL,我该如何在我的正则表达式中定义它?@vloryan首先,你可以随时打扰我。我自愿在这里工作,因为我想帮助你第二,你是想排除url的整个查询字符串,还是想拒绝包含查询字符串的整个url?
$in='<a href=\'http://www.twitter.com/singlequotes\'>Twitter Single Quotes</a>
<a href="http://www.facebook.com/doublequotes">Twitter Double Quotes</a>
<a href=\'http://twitter.com/singlequotes\'>Twitter Single Quotes</a>
<a href="https://www.facebook.com/doublequotes">Twitter Double Quotes</a>';

$companies=['twitter','facebook'];

$out=preg_match_all('/href *= *[\'"]\Khttps?:\/\/(?:www\.)?(?:'.implode('|',$companies).')\.com[^\'"]+/',$in,$out)?$out[0]:[];

var_export($out);