我是正则表达式的新手,我在Wordpress中遇到了这段代码,但我无法理解发生了什么,尽管有评论。请帮我搞清楚。
// catch base url
preg_match('/href="(.+?)"/i', $content, $matches);
$baseref = (is_array($matches) && !empty($matches)) ? $matches[1] : '';
// get the first image from content
preg_match('/<img.+?src="(.+?)"[^}]+>/i', $content, $matches);
$img_url = (is_array($matches) && !empty($matches)) ? $matches[1] : '';
这里是$ content包含的内容。
<![CDATA[<p>Buganda Road Chief Magistrate James Mawanda Eremye has released Makerere University administrator Edward Kisuze. The suspended administrator is accused of sexually harassing a student. Court told Kisuzze to pay cash bail of Shs2m and each of his three sureties Shs10m.</p>
<p><a href="http://matookerepublic.com/wp-content/uploads/2018/04/kisuze.png"><img class="alignnone wp-image-32386" src="http://matookerepublic.com/wp-content/uploads/2018/04/kisuze-300x175.png" alt="" width="680" height="396" srcset="http://matookerepublic.com/wp-content/uploads/2018/04/kisuze-300x175.png 300w, http://matookerepublic.com/wp-content/uploads/2018/04/kisuze-696x405.png 696w, http://matookerepublic.com/wp-content/uploads/2018/04/kisuze.png 720w" sizes="(max-width: 680px) 100vw, 680px" /></a></p>
<p>However, before releasing him the magistrate ordered the prosecutor to disclose to the defence the evidence to enable commencement of hearing of this case come <span data-term="goog_350196878">May 28 2018</span>.</p>
<p>On April 14, police arrested Kisuze after a viral picture of him kissing the student’s private parts in office was released online. On May 4<sup>,</sup> he appeared before court and was remanded to Luzira prison after pleading not guilty to charges.</p>
]]>
答案 0 :(得分:0)
第一个/href="(.+?)"/i
用于从href
标记中提取a
属性。
查看实例:https://regex101.com/r/84dhEk/2(绿色部分是匹配的)
第二个/<img.+?src="(.+?)"[^}]+>/i
用于从src
标记中提取img
属性。