Solved

Perl's $1 in PHP

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL limit and not so limited 13 41
Increase counter and attr inside a while loop 15 37
HTTPS for signup & login - HTTP for all other pages 10 51
Special characters in a TCPDF 4 27
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

733 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