Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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
PHP循环替换标记_Php_Html - Fatal编程技术网

PHP循环替换标记

PHP循环替换标记,php,html,Php,Html,我正在尝试为网站上的链接、颜色和项目符号做自定义标记,这样[l]…[/l]就会被里面的链接替换,[li]…[/li]就会被项目符号列表替换 我已经让它工作了一半,但是链接描述有问题,下面是代码: // Takes in a paragraph, replaces all square-bracket tags with HTML tags. Calls the getBetweenTags() method to get the text between the square tags func

我正在尝试为网站上的链接、颜色和项目符号做自定义标记,这样[l]…[/l]就会被里面的链接替换,[li]…[/li]就会被项目符号列表替换

我已经让它工作了一半,但是链接描述有问题,下面是代码:

// Takes in a paragraph, replaces all square-bracket tags with HTML tags. Calls the getBetweenTags() method to get the text between the square tags
function replaceTags($text)
{
  $tags = array("[l]", "[/l]", "[list]", "[/list]", "[li]", "[/li]");
  $html = array("<a style='text-decoration:underline;' class='common_link' href='", "'>" . getBetweenTags("[l]", "[/l]", $text) . "</a>", "<ul>", "</ul>", "<li>", "</li>");

  return str_replace($tags, $html, $text);
}

// Tages in the start and end tag along with the paragraph, returns the text between the two tags. 
function getBetweenTags($tag1, $tag2, $text)
{
  $startsAt = strpos($text, $tag1) + strlen($tag1);
  $endsAt = strpos($text, $tag2, $startsAt);

  return substr($text, $startsAt, $endsAt - $startsAt);
}
这些链接将被替换为:

http://www.example1.com
http://www.example1.com
http://www.example1.com
它们都是正确的超链接,即1,2,3,但所有链接的文本位都相同。
您可以在页面底部的三个随机链接中看到它的实际操作。如何更改代码以使正确的URL描述显示在每个链接下?这样每个链接都会正确地超链接到相应的页面,并显示相应的URL文本?

stru\u replace
为您完成所有的工作。问题在于:

getBetweenTags("[l]", "[/l]", $text)
不会改变。它将匹配3次,但只解析为
“http://www.example1.com“
因为这是页面上的第一个链接

您不能真正地进行静态替换,您需要至少保留一个指向您在输入文本中的位置的指针

我的建议是编写一个简单的标记器/解析器。其实没那么难。标记器可以非常简单,找到所有
[
]
并派生标记。然后您的解析器将尝试理解标记。您的令牌流可以如下所示:

array(
    array("string", "foo "),
    array("tag", "l"),
    array("string", "http://example"),
    array("endtag", "l"),
    array("string", " bar")
);

以下是我个人如何使用preg_match_all

$str='
[l]http://www.example1.com[/l] 
[l]http://www.example2.com[/l]
[l]http://www.example3.com[/l]
';
preg_match_all('/\[(l|li|list)\](.+?)(\[\/\1\])/is',$str,$m);
if(isset($m[0][0])){
    for($x=0;$x<count($m[0]);$x++){
        $str=str_replace($m[0][$x],$m[2][$x],$str);
    }
}
print_r($str);
$str='1〕
[l]http://www.example1.com[l]
[l]http://www.example2.com[l]
[l]http://www.example3.com[l]
';
preg|u match|u all('/\[(l|li | list)\](.+?)(\[\/\1\])/is',$str,$m);
如果(isset($m[0][0])){

对于($x=0;$x)您确定每次请求函数时参数都会更改吗?我认为发生的事情是,给定包含3个链接的整个段落,正确替换每个标记,但只调用getBetweenTags()标记一次,然后将该描述放在三个链接中的每一个上—我如何调整代码,使其在每次遇到一组新的方形标记时都告诉getBetweenTags()?
$str='
[l]http://www.example1.com[/l] 
[l]http://www.example2.com[/l]
[l]http://www.example3.com[/l]
';
preg_match_all('/\[(l|li|list)\](.+?)(\[\/\1\])/is',$str,$m);
if(isset($m[0][0])){
    for($x=0;$x<count($m[0]);$x++){
        $str=str_replace($m[0][$x],$m[2][$x],$str);
    }
}
print_r($str);