Newbie: preg_match

Hi to everyone, I just registrated.
I found this forum very helpful.
Yesterday I read someones post about preg_match which I tried to understand and implement in practice.
so this is my code.
I’m passing argument to some page to give me all links as text for video on that site. Idea is to pass these each value to another function which will again read source and give me final url on that domain from where I can download video.
Question is:
How can I slice this $matches var, either to put some xml attribute on each row or just slice and call another function.
Output from method is one big row text with no spaces and tags.

I tried with

 foreach($matches as $match){  echo
 $match; }

but it give me output like this:

<a href=“/video/michael-dunk-over1223”
class="michael-dunk-over1223>

…and so on for every row.
Again, I need row text for each $match.
Thanks in advance.

$mj23 = getVideo("michael-jordan");
  function getVideo($dir){
    $lines = file("http://www.somevideosite.com/{$dir}");
     foreach ($lines as $line_num => $line) {
       // $string = htmlspecialchars($line);      
         $pattern = '/<a href="\\/video\\/(.+)" class="img"/';  
 
         preg_match($pattern, $line, $matches);         
         echo $matches[1];     
     }       }

try this

$pattern = '/<a href="\\/video\\/(?<video>.+)" /s'; 

You’re trying to slice the attribute values out? I’m confused by what you want as output.

I tried with

$pattern = '/<a href="\\/video\\/(.+)" class="img"/ s'; 

putting the ‘s’ on the and but that did’nt work.
Patter itself it’s ok, but I need to slice this $matches var end for every sliced row->anotherfunction();

Sounds like you’re looking for array_map

Yes, I want to slice $matches.
with echo $matches[1]; I got one big row text file.
Idea is to get this row text but for every row.

Is it possible to add some xml tags on every row inside $matches.

Sorry is this question are simple, but I try to learn with practical examples.

If you’re going to use slashes in the pattern, don’t use them as pattern delimiters. Also, do you actually need to match class=“img”? Regexes are easier to deal with when you use the /x modifier as well.

$pattern = '@ <a \\s+ href=([\\'"]) /video/([^\\'"]+) \\1 @xi';

Of course, $2 would then hold the portion of the URL after “/video/”.

However, as you can see, trying to match various presentations of markup with regexes is a pain. That’s why you generally shouldn’t use them to parse HTML. Use the HTML parser that’s part of the PHP DOM library.

<?php

$doc = new DOMDocument();
$url = 'http://example.com/foobar';

/* Ignore markup errors */
if (@$doc->loadHTMLFile($url)) {
  $xpath = new DOMXPath($doc);
  $links = $xpath->query('//a[@href]');
  foreach ($links as $link) {
    $href = $link->getAttribute('href');
    /* parse URL, etc. */
  }
}

As for what you’re trying to do with the match, I still don’t understand what you want. Can you post a small sample of the actual data you’re working with, then post an example of the desired output you want.