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

Thiyagarajan
  • 247
  • 3
  • 5
  • 15

3 Answers3

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

hassan
  • 7,812
  • 2
  • 25
  • 36
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

vieroli
  • 366
  • 1
  • 5
  • 16
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);
Omolewa Stephen
  • 444
  • 3
  • 19