Solved

Perl's $1 in PHP

Posted on 2006-06-30
4
365 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 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