Solved

Perl's $1 in PHP

Posted on 2006-06-30
4
362 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 125 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

803 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