Delete part of a string and keep URL contained in it

Asked

Viewed 64 times

1

Inside a string there is a snippet used by Wordpress to display the image in the middle of a news, it is possible I remove all this stretch of the string and take advantage of the image URL?

$conteudonoticia = '[caption id="attachment_42478" align="alignleft" width="640"] <img class="wp-image-42478 size-full" src="http://paginalocal/wp-content/uploads/2016/12/20140815_174009.jpg" alt="20140815_174009" width="640" height="480" />Objetivo com a antecipação é garantir comodidade e tranquilidade aos servidores para as festas de final de ano[/caption]'

Elminiar everything else.

$urlimagem = 'http://paginalocal/wp-content/uploads/2016/12/20140815_174009.jpg'
  • see if it meets https://ideone.com/RVzHzC

3 answers

0

I made 2 functional examples, use what you think best, there must be other ways only I did just these.

var conteudo = '[caption id="attachment_42478" align="alignleft" width="640"] <img class="wp-image-42478 size-full" src="http://paginalocal/wp-content/uploads/2016/12/20140815_174009.jpg" alt="20140815_174009" width="640" height="480" />Objetivo com a antecipação é garantir comodidade e tranquilidade aos servidores para as festas de final de ano[/caption]';

alert(conteudo.split('src=')[1].split('"')[1]);
    

alert(jQuery('<div '+conteudo+'>').attr('src'));
  

    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

0

example with 1 url - ideone

example with 3 urls - ideone

With the use of regex

$conteudonoticia = '[caption id="attachment_42478" align="alignleft" width="640"] <img class="wp-image-42478 size-full" src="http://paginalocal/wp-content/uploads/2016/12/20140815_174009.jpg" alt="20140815_174009" width="640" height="480" />Objetivo com a antecipação é garantir comodidade e tranquilidade aos servidores para as festas de final de ano[/caption]';

preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $conteudonoticia, $match);

print_r($match[0]);

If you want string output replace print_r($match[0]); for

 $url = (implode(':', $match[0]));
 echo $url;

example - ideone

Documentation

0


@Leonardo Rafael Villas Bôas you can use regular expressions (regex).

following example: Take only the URL

In PHP would look like this:

$conteudonoticia = '[caption id="attachment_42478" align="alignleft" width="640"] <img class="wp-image-42478 size-full" src="http://paginalocal/wp-content/uploads/2016/12/20140815_174009.jpg" alt="20140815_174009" width="640" height="480" />Objetivo com a antecipação é garantir comodidade e tranquilidade aos servidores para as festas de final de ano[/caption][caption id="attachment_42478" align="alignleft" width="640"] <img class="wp-image-42478 size-full" src="http://paginalocal/wp-content/uploads/2016/12/20140815_174010.jpeg" alt="20140815_174009" width="640" height="480" />Objetivo com a antecipação é garantir comodidade e tranquilidade aos servidores para as festas de final de ano[/caption][caption id="attachment_42478" align="alignleft" width="640"] <img class="wp-image-42478 size-full" src="http://paginalocal/wp-content/uploads/2016/12/20140815_174011.png" alt="20140815_174009" width="640" height="480" />Objetivo com a antecipação é garantir comodidade e tranquilidade aos servidores para as festas de final de ano[/caption]';

$regex = '~(\[caption.*?\]).*?(http.*?\.[a-z]{3,4}).*?(\[\/caption\])~';

preg_match_all($regex, $conteudonoticia, $matches);
array_shift($matches);

$resul = [];
for ($aux = 0; $aux < count($matches); $aux++) {
    array_push($resul, implode(" ", array_column($matches, $aux)));
}

print_r($resul);

/* Resultado
Array
(
    [0] => [caption id="attachment_42478" align="alignleft" width="640"] http://paginalocal/wp-content/uploads/2016/12/20140815_174009.jpg [/caption]
    [1] => [caption id="attachment_42478" align="alignleft" width="640"] http://paginalocal/wp-content/uploads/2016/12/20140815_174010.jpeg [/caption]
    [2] => [caption id="attachment_42478" align="alignleft" width="640"] http://paginalocal/wp-content/uploads/2016/12/20140815_174011.png [/caption]
)
*/

Note: I made regex to accept the main image formats: .jpg, .png ou .jpeg. Any questions just ask.

  • It worked great, but as there was more than one image it picked up the url of all that were present. It would also be necessary to delete the [caption] .... [/caption] from the main content.

  • I changed the comment to suit the idea you just passed.

  • Solved. Thank you very much Djalma! : D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.