Solved

Perl's $1 in PHP

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now