A
A
alexledenev2020-05-07 05:29:35
PHP
alexledenev, 2020-05-07 05:29:35

How to remove everything except the image url?

Good afternoon, you need a regular expression that will find only the url of the image in the entire text, and delete everything else. Partially copes with this

$text = preg_replace('~(https?://\S+)|.~su', '$1', $text);
, but a little different :(
For example, there is a text:
dsfsodc123<123>sad https://i.imgur.com/mtbl1cr.jpg weqwe
As a result, it should display https://i.imgur.com/mtbl1cr.jpg
Please tell me second day suffering :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
anakondoz, 2020-05-07
@anakondoz

$text = preg_replace('~.*?(https.*jpg).*~g', '$1', $text);  //https://i.imgur.com/mtbl1cr.jpg

I
Ilya, 2020-05-07
@rpsv

$re = '/http(?:s?):\/\/.+?\.(?:jpg|jpeg|png)/s';
$str = '...';

preg_match_all($re, $str, $matches);
$urls = $matches[0];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question