Home > Backend Development > PHP Tutorial > 这条简单的正则如何写。

这条简单的正则如何写。

WBOY
Release: 2016-06-13 13:11:04
Original
740 people have browsed it

这条简单的正则怎么写。。

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php $pat = "!src=\"([^(\"|http)]*)\"!ieU";
$str = 'src="http://www" ';

if(preg_match($pat,$str)){
    echo "匹配成功";
}else{
    echo "失败";
}

Copy after login


我想匹配出 href= src= 这些如果不是以http开头就替换里面的内容, 是以http开头的就不替换
上面的正则哪里错了。。。。

------解决方案--------------------
你试下,我没发现问题
PHP code
$pat='/(src\=[\"\']?http)+|(href\=[\"\']?http)+/';
$str = 'src="http://www" ';

if(preg_match($pat,$str)){
    echo "匹配成功";
}else{
    echo "失败";
}
<br><font color="#e78608">------解决方案--------------------</font><br>方括号中的内容是一个个并列的字符,不是你这样成组使用的<br><br>你可能需要的是这样<br>
Copy after login
PHP code
$pat = '/src="(?!http).*"/iU';
$str = 'src="http://www" src="ftp://www"';
echo preg_replace($pat, 'xxx', $str);
<br><font color="#e78608">------解决方案--------------------</font><br>
像这样?非http:,http://www.开头的,补全URL?<br><br>
Copy after login
PHP code
$testurls = array(
    'http://www.freenewspos.com/italia/creativecommons-photo-album#!POS__calcio&num=0',
    'freenewspos.com/italia/creativecommons-photo-album#!POS__NBA&num=0',
    'www.freenewspos.com/italia/creativecommons-photo-album#!POS__Twilight&num=0'); 
foreach($testurls as $singleurl){
    echo preg_replace('#(?:http(s)?://)?(?:www\.)?(.+)#', 'http\1://www.\2', $singleurl).'<br>';
} <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template