質問

First I am getting html of the web page and then I am removing href links which normally appear on the left or right side of the page (not in the page body). Href links are being removed but their labels are not being removed.

Example:

<a href='http://test.blogspot.com/2012/11/myblog.html'>London</a>

Links is being removed but not it's label i.e. 'London'. How can I remove complete row in html source? I am using following code for it:

$string = strip_tags($html_source_code, '<a>', TRUE); 

function strip_tags($text, $tags = '', $invert = FALSE) {
      preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags); 
      $tags = array_unique($tags[1]); 
      if(is_array($tags) AND count($tags) > 0) { 
        if($invert == FALSE) { 
          return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text); 
        } 
        else { 
          return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text); 
        } 
      } 
      elseif($invert == FALSE) { 
        return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text); 
      } 
return $text; 
}
役に立ちましたか?

解決

If I use your code, I get a Fatal error: Cannot redeclare strip_tags().

Changing the name function to something like my_strip_tags works fine.

function my_strip_tags($text, $tags = '', $invert = FALSE) {
      preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags); 
      $tags = array_unique($tags[1]); 
      if(is_array($tags) AND count($tags) > 0) { 
        if($invert == FALSE) { 
          return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text); 
        } 
        else { 
          return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text); 
        } 
      } 
      elseif($invert == FALSE) { 
        return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text); 
      } 
return $text; 
}

$html_source_code = "Beginning of content ... <a href='http://test.blogspot.com/2012/11/myblog.html'>London</a> ... end of content.";

echo "<p>".$html_source_code."</p>";

$string = my_strip_tags($html_source_code, '<a>', TRUE);

echo "<p>".$string."</p>"; 

That prints:

Beginning of content ... London... end of content.

Beginning of content ... ... end of content.

他のヒント

$link = "<a href='http://test.blogspot.com/2012/11/myblog.html'>London</a>";

function erraser($theLink, $checkTag){

    if(strpos($theLink, $checkTag) == true){

        for($i=0; $i< strlen($theLink); $i++){
        $link[$i] = '';
        return  $link[$i];
        }
       }else{
        return $theLink;
    }

}

Now, lets look at this:

All you have to do it give the erraser() function two parameters, then link's variables, and any text to recognized the link by

If you do for ex: echo erraser($link, 'href'); it will delete the link, and return nothing. if you however give it ---- inside echo erraser($link, '----'); then, will give out the link london, meaning, it will check if it is a link or not and do the required function

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top