Delete part of a string and keep URL contained in it

1

Within a string there is an excerpt used by Wordpress to display the image in the middle of a story, is it possible for me to remove all of that portion of the string and take advantage of the URL of the image?

$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]'

Eliminate all the rest.

$urlimagem = 'http://paginalocal/wp-content/uploads/2016/12/20140815_174009.jpg'
    
asked by anonymous 22.06.2017 / 19:32

3 answers

0

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

follow example: Just get URL

In PHP it 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 the regex to accept the main image formats: .jpg, .png ou .jpeg . Any questions just ask.

    
22.06.2017 / 23:02
0

I've done 2 functional examples, use whatever you think is best, there must be other ways, but I've only done those.

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>
    
22.06.2017 / 20:02
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 to replace print_r($match[0]); with

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

example - ideone

Documentation

22.06.2017 / 20:20