Home > OS >  preg_match_all to find url in string
preg_match_all to find url in string

Time:12-07

I get may Data from XML file. I need to Output only the URL´s from the Strings in my array. In format like 'https://d1.cloudfront.net/00722.jpg' without the other tags and styles arround. I have try that with preg_match_all but i get no results. What i doing wrong?

public function xmlParserPICtn():string
{
    
    $valuesPICtn = $this->xml->xpath("//OBJEKT[@ID='91727']//PICTURE"); 
    $searchpattern="@SRC=(.*)width@";
    preg_match_all($searchpattern, $valuesPICtn, $valuesPICt); //Search-String
    foreach ($valuesPICt as $PICelements) 
      {
         $display .= '<li>';
         $display .= ''.$PICelements->PIC.'';
         $display .= '</li>';
            
        }
     $display .= '';

      return $display;
  }
  
  
  <?xml version="1.0" encoding="utf-8"?>
<OBJEKT ID="91727">
    
          <PICTURE ID="7">
              <ID>7</ID>
                  <PIC>&lt;IMG SRC="https://d1.cloudfront.net/00722.jpg" width="610" height="480" BORDER=0></PIC>
                 </PICTURE>
    
         <PICTURE ID="11">
              <ID>11</ID>
                  <PIC>&lt;IMG SRC="https://d1.cloudfront.net/01123.jpg" width="630" height="480" BORDER=0></PIC>
                 </PICTURE>
    
         <PICTURE ID="2">
                  <ID>2</ID>
                  <PIC>&lt;IMG SRC="https://d1.cloudfront.net/00224.jpg" width="740" height="480" BORDER=0></PIC>
                 </PICTURE>
    
         <PICTURE ID="9">
                  <ID>9</ID>
                  <PIC>&lt;IMG SRC="https://d1.cloudfront.net/00925.jpg" width="940" height="480" BORDER=0></PIC>
                 </PICTURE>
    
</OBJEKT>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Try this regex :

(?<=SRC=")(.*?)(?=\")

I get only the URL without the other tags.

You found here the demo

CodePudding user response:

You should loop the result of the xpath query that is in valuesPICtn

Then for every item in the loop, there is $PICelements->PIC that has a single picture. You can use preg_match instead, and take the group 1 value. Note that the second parameter of preg_match and preg_match_all take a string, where you tried to pass the return of the xpath call in your code.

Note that this part in your code $display .= ''; can be omitted as it concats an empty string.

The pattern SRC="([^"] )" is a slightly updated version, matching SRC=" and captures in group 1 any char other than a double quote

public function xmlParserPICtn():string
{
    $valuesPICtn = $this->xml->xpath("//OBJEKT[@ID='91727']//PICTURE");
    foreach ($valuesPICtn as $PICelements)
    {
        $searchpattern='@SRC="([^"] )"@';
        preg_match($searchpattern, $PICelements->PIC, $valuesPICt); //Search-String
        $display .= '<li>';
        $display .= $valuesPICt[1];
        $display .= '</li>';
    }
    return $display;
}

Php demo

  • Related