2015-12-16 3 views
2

Я выполнил некоторую работу, где я получаю строку из фрагментов результатов поиска Google API. В этой строке есть содержат <b> и </b> это делает проблему для дальнейшей работы, с тем, что мне нужно удалить <b> и </b>Удаление части строки из строки в PHP

Основной код строки:

$content_all .="#$*@#" . $json->responseData->results[$x]->content; 

После того как я попробовал этот код, чтобы удалить <b> и </b>:

$content_all=(string)$content_all; 
str_replace("<b>", " ", $content_all); 
str_replace("</b>", " ", $content_all); 

но не удалить <b> и </b>. Я попытался

str_replace("hello", " ", "Hello people"); 

Он работал. Но почему он не работает для $content_all укуса. я пишу значение $ content_all и это выглядит так

#$*@#These <b>sad songs</b> will definitely make you shed tears...unless you&#39;re a robot.#$*@#Check out our top 35 <b>songs</b> about heartbreak. Did we miss any? Let us know!#$*@#Feb 10, 2014 <b>...</b> &quot;Somewhere Somehow&quot; available on iTunes now! http://smarturl.it/somewhere- 
somehow Music video by We The Kings performing <b>Sad Song</b> ...#$*@#Nov 12, 2015 <b>...</b> There&#39;s nothing like a <b>sad</b>, slow <b>song</b> to aid in a postbreakup cry or to be the 
soundtrack to a bad day. It&#39;s a well-known fact that music is made ...#$*@#May 12, 2015 <b>...</b> Yes, <b>sad songs</b> do say so much. And these 50 songs helped the Paste staff to 
hurt so good. I tried to keep it to one song per artist but Johnny ...#$*@#Just Cry, <b>Sad Songs</b>. Indiemono. The saddest and original playlist on Spotify, &#39; 
Just Cry, <b>Sad Songs</b>&#39;, it&#39;s been active for 3 years, time of ... Song, Artist, Album ...#$*@#<b>sad songs</b>, <b>sad song</b>, saddest song in the world, world´s saddest song, battle 
song, sadsong, love song.#$*@#Last month we made a playlist of the 50 most uplifting songs ever. Now, we look 
at the opposite: 50 beautifully <b>sad songs</b>, beginning with Amy Winehouse ...#$*@#Are you looking for the best <b>sad songs</b>? Look no further, this post contains our 
favorite 20 <b>sad songs</b> of all time (as of 2015)!#$*@#8tracks radio. Online, everywhere. - stream 2500+ <b>sad songs</b> playlists including 
sad, Coldplay, and indie music from your desktop or mobile device.#$*@#Nov 6, 2015 <b>...</b> We will also be engaging in the ritual of indulging our completely unnecessary, 
sun-affected sadness by playing sad, <b>sad songs</b> on infinite ...#$*@#&quot;<b>Sad Songs</b> (Say So Much)&quot; is a song by Elton John and is the closing track on 
the 1984 album Breaking Hearts. It reached the No 5 on the U.S. chart. The lyrics 
 ...#$*@#<b>Sad Songs</b> for Dirty Lovers is the second studio album by indie rock band The 
National. It was released in 2003 on Brassland Records. This is the first album on 
 ...#$*@#Sep 24, 2015 <b>...</b> The popular belief that a <b>song</b> can be so <b>sad</b> that it can trigger suicide has a long 
history. Written in 1933 by Hungarian composer Seress and ...#$*@#The National - <b>Sad Songs</b> for Dirty Lovers - Amazon.com Music.#$*@#<b>Sad Songs</b> 1 Songs download Free Music Latest New Top Best Hit Play Trend 
Albums Single djjohal com 20 online dj remix. 
+0

Что такое полное содержание $ content_all – Perrykipkerrie

+0

Я Эдите значение – SOURAV

+0

Как ответил Marcoveccio, вы на самом деле сделать str_replace, но вы сделали не сохранять результат. Поэтому вы не сделали то, что хотели – Perrykipkerrie

ответ

5

Вы должны сделать это таким образом:

$content_all=str_replace("<b>", " ", $content_all); 
$content_all=str_replace("</b>", " ", $content_all); 

echo $content_all; 

Конечно, последняя строка просто эхо, так что вы можете увидеть результат.

+0

Thats correct !! –

6

Если вы хотите удалить HTML теги из строки вы можете пойти на strip_tags()

например:

<?php 
    echo strip_tags("Hello <b>world!</b>");//Hello world 
    ?> 

Будьте осторожны strip_tag удалит все теги из строки.

http://www.w3schools.com/php/func_string_strip_tags.asp.

еще одна вещь, если вы используете str_replace("<b>", " ", $content_all);, то это даст нежелательное пространство

ie:str_replace("<b>", " ", "hi <br><br> vig"); 
result: hi  vig 
Смежные вопросы