Php 正则表达式

Php 正则表达式,php,regex,preg-replace,Php,Regex,Preg Replace,我们希望使用preg_replace进行查找和替换。但无法达到预期的效果 这是我的绳子 $x = '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/11/i_leave_shreds_.html#comment-11657412">FALLACI</a>'; $x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_sh

我们希望使用preg_replace进行查找和替换。但无法达到预期的效果

这是我的绳子

    $x = '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/11/i_leave_shreds_.html#comment-11657412">FALLACI</a>';
    $x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/10/i_leave_shreds_.html#comment-11657412">FALLACI</a>';
    $x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/20>';05/1/i_leave_shreds_.html#comment-11657412">FALLACI</a>';
    $x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/9/i_leave_shreds_.html#comment-11657412">FALLACI</a>';
    $x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2006/11/i_leave_shreds_.html#comment-11657412">FALLACI</a>';
    $x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/i_leave_shreds_.html#comment-11657412">FALLACI</a

    echo preg_replace('/<a(.*?)href="http:\/\/atlasshrugs2000.typepad.com\/atlas_shrugs\/([0-9\/]{0,7}?)(.*?)_.html#(.*?)"(.*?)>/','<a$1href="http://localhost/test/$3#$4"$5>',$x);
$x='';
$x.='';
$x.='';
$x.='';
$x.='';
$x='
但我们想要这样的结果

<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>

请帮帮我。
提前感谢:)

问题在这里:
([0-9\/]{0,7}?
。。。您有0-7个实例,然后您希望获得尽可能少的实例。您不需要同时指定这两个。。。是否删除?最后(看起来像是
([0-9\/]{0,7})
),然后它就可以工作了

试试看:
/


{0,7}?(
更改为
{0,7})\/(

解决方案 如果我们从排列你当前的正则表达式模式开始

这:

此正则表达式与原始正则表达式之间的差异是(上面要点列表的第4行):


您是否尝试过使用而不是正则表达式?如果href中有其他url,则不应替换它。我们只希望在href包含此url时替换它“”.好的,我已经用更具体的regex模式和解释更新了答案…非常感谢您的宝贵帮助。您能给我推荐一个可以学习regex的链接吗?单击顶部栏菜单中的
教程
,这里有关于regex的所有解释:)虽然在数据较少的情况下,其工作正常。但当我们在数据库文件中执行相同的操作时,会产生内存大小错误。在数据库文件中执行相同操作的最佳方法是什么。谢谢
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
$x = '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/11/i_leave_shreds_.html#comment-11657410">FALLACI</a>';
$x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/10/i_leave_shreds_.html#comment-11657411">FALLACI</a>';
$x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/1/i_leave_shreds_.html#comment-11657412">FALLACI</a>';
$x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/9/i_leave_shreds_.html#comment-11657413">FALLACI</a>';
$x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2006/11/i_leave_shreds_.html#comment-11657414">FALLACI</a>';
$x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/i_leave_shreds_.html#comment-11657415">FALLACI</a>';

echo preg_replace('~<a.*?href=["\'].*?/([^/]*?)_\.html#(.*?)["\'].*?>(.*?)</a>~', "<a href='http://localhost/test/$1#$2'>$3</a><br>\n", $x);
<a href='http://localhost/test/i_leave_shreds#comment-11657410'>FALLACI</a><br>
<a href='http://localhost/test/i_leave_shreds#comment-11657411'>FALLACI</a><br>
<a href='http://localhost/test/i_leave_shreds#comment-11657412'>FALLACI</a><br>
<a href='http://localhost/test/i_leave_shreds#comment-11657413'>FALLACI</a><br>
<a href='http://localhost/test/i_leave_shreds#comment-11657414'>FALLACI</a><br>
<a href='http://localhost/test/i_leave_shreds#comment-11657415'>FALLACI</a><br>
~<a.*?href=["'].*?/([^/]*?)_\.html#(.*?)["'].*?>(.*?)</a>~
~<a.*?href=["\'].*?atlasshrugs2000.typepad.com.*?/([^/]*?)_\.html#(.*?)["\'].*?>(.*?)</a>~
.*?/                                <-- Original
.*?atlasshrugs2000.typepad.com.*?/  <-- Updated
<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/11/i_leave_shreds_.html#comment-11657410">FALLACI</a>
<a href="atlasshrugs2000.typepad.com/atlas_shrugs/2005/11/i_leave_shreds_.html#comment-11657410">FALLACI</a>
<a href="https://atlasshrugs2000.typepad.com/atlas_shrugs/2005/11/i_leave_shreds_.html#comment-11657410">FALLACI</a>