由于自PHP 5.3.0以来不推荐使用POSIX正则表达式(ereg),因此我想知道将旧表达式转换为PCRE(Perl兼容正则表达式)(preg)的简单方法.
每个例子,我有这个正则表达式:
eregi('^hello world');
Run Code Online (Sandbox Code Playgroud)
如何将表达式转换为preg_match兼容表达式?
注意:此帖子用作与从ereg转换为preg相关的所有帖子的占位符,以及相关问题的重复选项.请不要关闭这个问题.
有关:
这个正则表达式我得到了一个错误..
$strTmp = preg_replace('~(<\/CharacterStyleRange>(.*?)\n*</CharacterStyleRange>)~gim ' , "</CharacterStyleRange>", $strTmp);
Run Code Online (Sandbox Code Playgroud)
错误
警告:preg_replace():未知的修饰符'g'在....
为什么?
$result = preg_replace(
"/\{([<>])([a-zA-Z0-9_]*)(\?{0,1})([a-zA-Z0-9_]*)\}(.*)\{\\1\/\\2\}/iseU",
"CallFunction('\\1','\\2','\\3','\\4','\\5')",
$result
);
Run Code Online (Sandbox Code Playgroud)
上面的代码在升级到PHP 5.5后给出了弃用警告:
不推荐使用:preg_replace():不推荐使用/ e修饰符,而是使用preg_replace_callback
如何更换代码preg_replace_callback()?
好吧,所以我正在研究解析RSS提要.我得到了我不需要的数据,我剩下的就是解析游戏标题了.
这是我目前的代码(忽略了邋,,它只是一个概念证明):
<?php
$url = 'http://raptr.com/conexion/rss';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($result);
$lastgame = $xml->channel->item[0]->description;
preg_match('[a-zA-Z]+</a>.$', $lastgame, $match);
echo $match;
?>
Run Code Online (Sandbox Code Playgroud)
一切都很好,但后来我开始收到这个错误:
Warning: preg_match() [function.preg-match]:
Unknown modifier '+' in raptr.php on line 14
Run Code Online (Sandbox Code Playgroud)
我唯一剩下的就是去掉封闭的锚标签和句号,但我似乎无法弄清楚为什么它不喜欢'+'.有任何想法吗?
编辑:这不应该被标记为重复,因为在另一个问题之前两年被问到.
我一直收到这个错误:
警告:preg_match()[function.preg-match]:第235行的D:\ xampp\htdocs\administrator\components\com_smms\functions\plugin.php中的未知修饰符't'
上:
$PageContent = preg_replace($result->module_pregmatch, '', $PageContent);
Run Code Online (Sandbox Code Playgroud)
我在$ result-> module_pregmatch上做了一个var_dump,得到以下结果:
string '/<title>(.*)</title>/Ui' (length=23)
string '/<meta[^>]*name=["|\']description["|\'][^>]*content=["|\'](.*)["|\']\s*\/>/Ui' (length=77)
string '/<meta[^>]*name=["|\']keywords["|\'][^>]*content=["|\'](.*)["|\']\s*\/>/Ui' (length=74)
string '/<meta[^>]*name=["|\']author["|\'][^>]*content=["|\'](.*)["|\']\s*\/>/Ui' (length=72)
string '/<meta[^>]*name=["|\']copyright["|\'][^>]*content=["|\'](.*)["|\']\s*\/>/Ui' (length=75)
string '/<meta[^>]*name=["|\']robots["|\'][^>]*content=["|\'](.*)["|\']\s*\/>/Ui' (length=72)
string '/<meta[^>]*http=equiv=["|\']content-language["|\'][^>]*content=["|\'](.*)["|\']\s*\/>/Ui' (length=88)
string '/<meta[^>]*http-equiv=["|\']content-type["|\'][^>]*content=["|\'](.*)["|\']\s*\/>/Ui' (length=84)
string '/<link[^>]*href=["|\'](.*)["|\'][^>]*rel=["|\']shortcut[^>]*icon["|\'][^>]*type=["|\']image\/x-icon["|\']\s*\/>/Ui' (length=114)
string '/<link[^>]*href=["|\'](.*)["|\'][^>]*rel=["|\']alternate["|\'][^>]*type=["|\']application\/rss\+xml["|\'][^>]*title=["|\'](.*)["|\'][^>]\/>/Ui' (length=142)
string '/<link[^>]*href=["|\'](.*)["|\'][^>]*rel=["|\']alternate["|\'][^>]*type=["|\']application\/atom\+xml["|\'][^>]*title=["|\'](.*)["|\'][^>]\/>/Ui' (length=143)
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我我做错了什么吗?我一直坚持这个错误太久了...
我正在编写虚假的电子邮件地址,我只是想确保它们是有效的电子邮件格式,所以我试图删除下面集合中没有的任何字符:
$jusr['email'] = preg_replace('/[^a-zA-Z0-9.-_@]/g', '', $jusr['email']);
Run Code Online (Sandbox Code Playgroud)
我在我的Windows机器上没遇到任何问题,但是在linux dev服务器上,每次运行此代码时都会出现此错误:
Warning: preg_replace() [function.preg-replace]: Unknown modifier 'g' in /var/www/vhosts/....
Run Code Online (Sandbox Code Playgroud)
我认为这是正则表达式字符串,但我不能把它固定下来.帮助不大?谢谢.
澄清:我不是想要容纳所有有效的电子邮件地址(我的目的不必要),我只需要弄清楚我的preg_replace正则表达式有什么问题.
我正在尝试使用preg_match来返回页面源代码中包含在""中的所有URL.
我正在使用的代码是
preg_match('"http://(.+?)\"', $code, $matches);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Warning: preg_match() [function.preg-match]: Unknown modifier '/' in .... on line 13
Run Code Online (Sandbox Code Playgroud) 假设$ body等于
something
that
does
not
interest
me
<!-- start -->
some
html
code
<!-- end -->
something
that
does
not
interest
me
Run Code Online (Sandbox Code Playgroud)
如果我使用
$body=preg_replace("(.*)<!-- start -->(.*)<!-- end -->(.*)","$2",$body);
Run Code Online (Sandbox Code Playgroud)
我获得:
something
that
does
not
interest
me
<!-- start -->
some
html
code
<!-- end -->
something
that
does
not
interest
me
Run Code Online (Sandbox Code Playgroud)
我该如何纠正?
我试图在页面上搜索某些内容,但我不断收到这个愚蠢的错误
这是我收到的错误
警告:preg_match() [function.preg-match]:未知修饰符“d”
这是我使用的代码
$qa = file_get_contents($_GET['url']);
preg_match('/<a href="/download.php\?g=(?P<number>.+)">Click here</a>/',$qa,$result);
Run Code Online (Sandbox Code Playgroud)
$_GET['url'] 可以等同于很多东西,但在一种情况下它是http://freegamesforyourwebsite.com/game/18-wheeler--2.html
该 url 的 html 基本上
有人知道吗:S?我什至不知道从哪里开始,因为我不知道 modifire 是什么,而且 php.net 网站也没有帮助
谢谢 !
我有那个测试工作正常:
if (ereg("([0-9]{2})[-./]([0-9]{2})[-./]([0-9]{4})[ ]([0-9]{2}):([0-9]{2}):([0-9]{2})", $dateToTest, $tab) == false)
Run Code Online (Sandbox Code Playgroud)
并且随着ereg的弃用,我用这个替换了测试:
if (preg_match("/([0-9]{2})[-./]([0-9]{2})[-./]([0-9]{4})[ ]([0-9]{2}):([0-9]{2}):([0-9]{2})/", $dateToTest, $tab) == false)
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
Warning: preg_match() [function.preg-match]: Unknown modifier '.' in ..................
Run Code Online (Sandbox Code Playgroud)
有什么问题,我该如何解决?