?
Solved

Perl's $1 in PHP

Posted on 2006-06-30
4
Medium Priority
?
369 Views
Last Modified: 2010-05-18
I have fairly good at perl programming but I have a project in PHP.

I want to use a regex to grab something in the middle of the string, such as I would with $1 in perl.

Example line that will be matched:

<tr><td><center><a href="/linky.html">Blah Blah</a></center></td><td><center>Private</center></td><td><center></center></td><td><center></center></td><td><center></center></td></tr>

All I want to get is the /linky.html out of that string, and into another string.

CODE:
$lines = file($file);
      foreach ($lines as $line_num => $line) {
            $match = preg_split('/<a href="/', $line,-1,PREG_SPLIT_OFFSET_CAPTURE);
            print_r($match);
      }

I'm getting HTML from an external website and parsing it for links pretty much.
0
Comment
Question by:undy30
  • 3
4 Comments
 
LVL 29

Expert Comment

by:TeRReF
ID: 17022818
<?php

$s = '<tr><td><center><a href="/linky.html">Blah Blah</a></center></td><td><center>Private</center></td><td><center></center></td><td><center></center></td><td><center></center></td></tr>'; preg_match('/href=\"(.*?)\">/', $s, $matches);
print($matches[1]);

?>
0
 
LVL 29

Accepted Solution

by:
TeRReF earned 500 total points
ID: 17022833
If there is more than 1 match in a string, you can use preg_match_all();
<?php

$s = '<tr><td><center><a href="/linky.html">Blah Blah</a></center></td><td><center>Private</center></td><td><center></center></td><td><center></center></td><td><center></center></td></tr><a href="/linky2.html">blah2</a>';
preg_match_all('/href=\"(.*?)\">/', $s, $matches);
// it's the second element of each array in the multidimensional array
print_r($matches);

?>

0
 

Author Comment

by:undy30
ID: 17026939
Thanks, the preg_match_all worked.

I tried that once but it didn't work for some reason, I did it a bit different than you though.

Thanks!
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 17026996
You're welcome.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question