-6

I want to convert this javascript regex to php regex. I have test the code can run.

<img.*?src='(?!http[s]*:\/\/img.[a-zA-Z0-9]*.test\/)(.*?)'

<img alt='' src='http://img.dev.teeeest/images/UID' />
<img alt='' src='https://img.deaaav.test.com/images/UID/' />
<img alt='' src='http://api.com/images/UID' />
<img alt='' src='http://img.deaSassav.test/images/UID' />
<img alt='' src='https://img.dev.test/images/UID' />

It will choose first three of these. I want php regex will have the same result.

$body = "<img alt='' src='http://img.dev.teeeest/images/UID' />
<img alt='' src='https://img.deaaav.test.com/images/UID.ux/' />
<img alt='' src='http://api.com/images/UID' />
<img alt='' src='http://img.deaSassav.test/images/UID' />
<img alt='' src='https://img.dev.test/images/UID' />";
$body = stripslashes($body);
$img_array = array();
preg_match_all('/<img[^>]*?src=[\'|\"](http[s]*:\/\/img.[a-zA-Z0-9]*.test\/[^"|^\']*?)[\'|\"][^>]*?>/isU', $body, $img_array);
$img_array = array_unique($img_array[1]);
echo "<pre>";
print_r($img_array);
echo "</pre>";

i use the website test https://regex101.com/ i attempt use add an ^ before http[s]. like this

<img[^>]*?src=[\'|\"](^http[s]*:\/\/img.[a-zA-Z0-9]*.test\/)[^"|^\']*?[\'|\"][^>]*?>

i want to negative what have this

http[s]*:\/\/img.[a-zA-Z0-9]*.test\/
Night
  • 17
  • 3
  • i just don't know how to write it... i try a lot function. but do't have the same result – Night Aug 19 '19 at 02:57
  • Can you include the functions you said you tried, but did not get the correct result with? – Spangle Aug 19 '19 at 02:59
  • it is my first question on stackoverflow. maybe have some false expression sorry – Night Aug 19 '19 at 02:59
  • It is clear to understand what you are wanting to do, however, it is hard to assist you if you do not include your attempts. – Spangle Aug 19 '19 at 03:02
  • ]*?src=[\'|\"](http[s]*:\/\/img.[a-zA-Z0-9]*.ux\/)[^"|^\']*?[\'|\"][^>]*?> this is my php regex. it can choose the last two. but i want the others – Night Aug 19 '19 at 03:02
  • Always think about the problem thoroughly and constructively before posting. Do you think this kind of conversion is logical, possible or beneficial? – Udo E. Aug 19 '19 at 03:03
  • Please add the above to your question. Also include some more php, so we can run the code. – Spangle Aug 19 '19 at 03:03
  • ok i edit. sorry – Night Aug 19 '19 at 03:05
  • the php code can run. but can't get what i want result. i think just lack some expression. but don't know how to write – Night Aug 19 '19 at 03:12

2 Answers2

2

Your javascript regex works fine; you just need to change the s flag to m:

$body = "<img alt='' src='http://img.dev.teeeest/images/UID' />
<img alt='' src='https://img.deaaav.test.com/images/UID.ux/' />
<img alt='' src='http://api.com/images/UID' />
<img alt='' src='http://img.deaSassav.test/images/UID' />
<img alt='' src='https://img.dev.test/images/UID' />";
$body = stripslashes($body);
$img_array = array();
preg_match_all("/<img.*?src='(?!http[s]*:\/\/img.[a-zA-Z0-9]*.test\/)(.*?)/imU", $body, $img_array);
echo "<pre>";
print_r($img_array[0]);
echo "</pre>";

Output:

<pre>Array
(
    [0] => <img alt='' src='http://img.dev.teeeest/images/UID' />
    [1] => <img alt='' src='https://img.deaaav.test.com/images/UID.ux/' />
    [2] => <img alt='' src='http://api.com/images/UID' />
)
</pre>

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
0

This expression might also work:

<img\b.*?\bsrc=['"](?!https?:\/\/img\.[a-z0-9]*\.test\/)([^'"]*)

If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


Emma
  • 27,428
  • 11
  • 44
  • 69