Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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
regex-php:“引用;摆脱[link1]摆脱[link2]。。。除掉问题';摆脱';当没有[链接]_Php_Regex_Preg Replace_Regex Group - Fatal编程技术网

regex-php:“引用;摆脱[link1]摆脱[link2]。。。除掉问题';摆脱';当没有[链接]

regex-php:“引用;摆脱[link1]摆脱[link2]。。。除掉问题';摆脱';当没有[链接],php,regex,preg-replace,regex-group,Php,Regex,Preg Replace,Regex Group,如何用一行preg_replace()实现以下输出 $string1="get rid1 [link1] get rid2 [link2] ..."; // any number of links echo "[<a href=link1>link1</a>][<a href=link2>link2</a>]"; $string2="get rid any text any text get rid"; // = no links: is a po

如何用一行preg_replace()实现以下输出

$string1="get rid1 [link1] get rid2 [link2] ..."; // any number of links
echo "[<a href=link1>link1</a>][<a href=link2>link2</a>]";
$string2="get rid any text any text get rid"; // = no links: is a possibility
echo "";
$string1=“获取rid1[link1]获取rid2[link2]…”;//任意数量的链接
回声“[[]”;
$string2=“删除任何文本任何文本删除”/=没有链接:这是一种可能性
回声“;
我尝试了以下方法,例如$string1,但不适用于上面的$string2:

$regex="/".
"[^\[\]]*". // the non-bracketed text before: -> eliminate
"\[(.*?)\]". // the bracketed text: [.]: -> convert into links 
"[^\[\]]*"; // get rid of non-bracketed text after: -> eliminate
"/";
echo preg_replace($regex,'<a href=jp.php?jp=\1>[\1]</a>',$string1);
$regex=“/”。
"[^\[\]]*". // 前面的无括号文本:->删除
"\[(.*?)\]". // 括号内的文本:[.]:->转换为链接
"[^\[\]]*"; // 删除:->消除后的无括号文本
"/";
echo preg_替换($regex,,$string1);
我认为非捕获组
(?:…)
可能会起作用,但我不明白…

为什么不只是如果

if ($output = preg_replace($regex,'<a href=jp.php?jp=\1>[\1]</a>',$string1))
echo $output;

$output=preg_replace($regex,,$string1);
如果($output!=$string1)
echo$输出;

我被这个“摆脱”弄糊涂了。总是“摆脱”吗?或者是别的什么?不清楚。你说如果没有“[链接]”它就会失败,那么就不会有任何输出。。。失败的地方是什么?“摆脱”可以是任何没有括号的文本,比如“废话废话废话废话废话废话废话废话废话废话废话废话废话废话废话废话。现在清楚了吗我已经修改了原来的问题,见上文:……它确实有效!“朝阳(参见[幹]) + [乙]奇数“正确链接”[幹][乙]"是的,但您只想替换链接,这样它也会删除您不想影响的文本…?作为我最后一条评论的输出正是我想要的,但如果没有“[。]我会努力处理空输出“包含构造……不要,空输出意味着不匹配,只需使用我首先给出的if语句。实际上,将第一个‘if’构造应用于$string1=”针头+嘴>关节”会导致“针头+嘴>关节”而不是空文本。
preg_replace("(text we dont want to replace)(text we do want to replace)(more junk text)",$1." altered $2 = ".$2." ".$3, $string1)
$output = preg_replace($regex,'<a href=jp.php?jp=\1>[\1]</a>',$string1);
if ($output != $string1)
echo $output;