1

I need to extract the all url's from the string using php , I refered below url but not getting exact result I want. Reference url and my string are below,

$string = "hi new image one http://xxx/images/c4ca4238a0b923820dcc509a6f75849b208754572.jpgand two arehttp://yyy/images/c1f1a611c1147c4054c399c01f8bad76686484492.jpgend"; $regex = '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#'; preg_match_all($regex, $string, $matches); echo "<pre>"; print_r($matches[0]); 

am getting result are

Array ( [0] => http://xxx/images/c4ca4238a0b923820dcc509a6f75849b208754572.jpgand ) 

It shows only one result , but in string 2url's are available, is it possible to get below result,

Array ( [0] => http://xxx/images/c4ca4238a0b923820dcc509a6f75849b208754572.jpg [1] => http://yyy/images/c1f1a611c1147c4054c399c01f8bad76686484492.jpg ) 

How to remove appending text in front and end of url and filter exact url's from string ? Any help Appreciated

14
  • Are these real url ? Commented Sep 25, 2018 at 9:44
  • no, just I replaced host address to xxx and yyy Commented Sep 25, 2018 at 9:45
  • But the end of the url, they are what you have as input url ? Commented Sep 25, 2018 at 9:47
  • 1
    Use @http://[^\s]+.jpg@ Commented Sep 25, 2018 at 9:52
  • 1
    @mohammad using '@http://[^\s]+.(jpg|png|gif|swf|jpeg)@' is resolved m issue Commented Sep 25, 2018 at 10:38

3 Answers 3

1

The problem is in you are matching a link with a boundary of the http word

$regex = '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#'; // ^^ note this 

omitting the boundary will get the full list of urls in your string

$regex = '#https?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#'; 

will output:

Array ( [0] => http://xxx/images/c4ca4238a0b923820dcc509a6f75849b208754572.jpgend [1] => http://yyy/images/c1f1a611c1147c4054c399c01f8bad76686484492.jpgand ) 

You SHOULD match against some fixed suffix in the end of the url.

I will assume that you want to match against jpg,jpeg,png images , so your pattern may look like:

$regex = '#https?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/)\.(jpg|jpeg|png))#'; 

Live example: https://3v4l.org/WACo1

Sign up to request clarification or add additional context in comments.

1 Comment

yeah great and simple one, +1 for answer
0

You can make a for loop. With the size of the array $matches And then print the result.

<?php $string = "hi new image one http://xxx/images/c4ca4238a0b923820dcc509a6f75849b208754572.jpgand two are http://yyy/images/c1f1a611c1147c4054c399c01f8bad76686484492.jpgend"; $regex = '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#'; preg_match_all($regex, $string, $matches); echo "<pre>"; for($i=0;$i<sizeof($matches);$i++){ print_r($matches[$i]); } 

Try this, and let me know if it match your needs

1 Comment

it also give exact result , but hassan gives simple solution
0

Here is the answer to your question

 $string = "hi new image one http://xxx/images/c4ca4238a0b923820dcc509a6f75849b208754572.jpg and two are http://yyy/images/c1f1a611c1147c4054c399c01f8bad76686484492.jpg end"; $strArray = explode(' ', $string); $newString = ""; $url = array(); foreach($strArray as $word) { if (substr($word, 0, 7) == "http://" || substr($word, 0, 8) == "https://") { $url[] = $word; } else { if ($newString != '') $newString .= ' '; $newString .= $word; } } print_r($url); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.