删除不确定的字符串块(URL)

时间:2014-12-18 06:34:03

标签: php string anchor strip

我有一个像这样的JSON数组:

[
    {
        "title": " Bones of the Hills <a href="/series/44108-conqueror" class="greyText">(Conqueror #3) </a>",
        "author": "Conn Iggulden",
        "format": "Hardcover",
        "pages": "518",
        "rating": "4.29",
        "image": "http://d.gr-assets.com/books/1347616868l/3276637.jpg"
    }
]

有时,(并非总是)标题字符串中包含链接。如何删除链接及其中的所有内容?我甚至不希望锚内的文本。我想最终以JUST“The Bones of the Hills”为标题。我怎么能在PHP中这样做?我用google搜索负载,但似乎没什么用。

3 个答案:

答案 0 :(得分:0)

你可以在标题上使用php内置函数strip_tags(),它将从标题中删除所有html元素,包括链接

答案 1 :(得分:0)

您可以使用strip_tag()函数来避免此错误

 $str='[{"title": " Bones of the Hills <a href="/series/44108-conqueror" class="greyText">(Conqueror #3) </a>","author": "Conn Iggulden","format": "Hardcover",';
        $str.='"pages": "518","rating": "4.29","image": "http://d.gr-assets.com/books/1347616868l/3276637.jpg"}]';
  echo      $str2=strip_tags($str);
  $array=json_decode($str2);
print_r($array);

答案 2 :(得分:0)

首先你的json数组不正确,请检查

[
    {
        "title": " "title": " Bones of the Hills <a href=\\"/series/44108-conqueror\\" class=\\"test\\">(Conqueror #3) </a>",
        "author": "Conn Iggulden",
        "format": "Hardcover",
        "pages": "518",
        "rating": "4.29",
        "image": "http://d.gr-assets.com/books/1347616868l/3276637.jpg"
    }
]

使用"title": " Bones of the Hills <a href=\\"/series/44108-conqueror\\" class=\\"test\\">(Conqueror #3) </a>"

而不是

<a href="/series/44108-conqueror" class="greyText"

然后

$arr = json_decode($json,true);
$arr['title'] = preg_replace("/<a.+?href.+?>.+?<\/a>/is","",$arr['title']);

var_dump($arr['title']);

现在你的头衔将是

string(20) " Bones of the Hills "