• Home
  • Amoeba
  • ASK
  • Community
  • Source Codes
  • Contact Us
Blue Orange Green Pink Purple

Regular Expression in PHP – Find Link Texts

Posted in PHP/MySQL, Programming Techniques, Regular Expression. on Friday, January 29th, 2010 by Aneeska Tags: extracting links, link href, link text, pattern matching, php, regex, regexp, Regular Expression
Jan 29

A small article for those who want to experience with Regular Expressions in PHP. Regular expressions, also referred to as regex or regexp, provide a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters. Regular expressions are used by many text editors, utilities, and programming languages to search and manipulate text based on patterns.

OK. Let’s now try a small example. Let’s try to find the URL defined in the HREF attribute and the Link Text in all the tags present in an HTML string.

This is the HTML we have:

<html>
<body>
<a href=”http://www.google.com”>Google</a>
<a href=”http://www.yahoo.com”>Yahoo</a>
</body>
</html>

We will now find the href value and the link text in the above html code. So we are expecting an output like this.

http://www.google.com – Google
http://www.yahoo.com – Yahoo

Here is the Regular Expression for this.

preg_match_all("/\<a.*href=\"(.*?)\".*?\>(.*)\<\/a\>+/", $yourhtmlstring, $matches, PREG_SET_ORDER);
PREG_SET_ORDER is used order results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on.

All the matchings found will be returned in the $matches array. Let’s see the content of the $matches array.

Array
(
    [0] => Array
        (
            [0] => Google
            [1] => http://www.google.com
            [2] => Google
        )

    [1] => Array
        (
            [0] => Yahoo
            [1] => http://www.yahoo.com
            [2] => Yahoo
        )

)

A simple script for you to try:

<?php
if(count($_POST)) {
	preg_match_all("/\<a.*href=\"(.*?)\".*?\>(.*)\<\/a\>+/", stripslashes($_POST['data']), $matches, PREG_SET_ORDER);
	foreach($matches as $key=>$match) {
		echo htmlentities($match[2]).' : '.$match[1]."<br/>";
	}
}
?>
<br/>
<br/>
<form action="" enctype="multipart/form-data" method="post">
<textarea name="data" rows="10" cols="100"></textarea><br>
<input type="submit" name="submit"/>
</form>

This script when run shows a text area where you can paste your html code with <a> tags in it. Submit the form and you can see the links extracted.

More articles on Regular Expression coming soon!

Enjoy!


ASK a Question!

Bookmark and Share

4 Comments

  1. suraj on July 9th, 2010

    it is not working for me

  2. Aneeska on July 9th, 2010

    Hi Suraj,

    There was an error in the code as some characters appeared as html entities.

    I have updated the code above.

    Try and let me know.

    Regards,
    Anees

  3. suraj on July 12th, 2010

    Thanks for your kind reply.
    This is what I wanted .Thank you…. ;)

  4. Aneeska on July 13th, 2010

    Great to know Suraj. Have fun!



Leave a Reply

CAPTCHA Image CAPTCHA Audio
Refresh Image
  • Search a Topic


  • Got a Question?
      ASK and get a Solution! ASK
  • Categories
    • CakePHP
    • Computers
    • CSS & XHTML
    • Enterprise Web
    • Flash & Action Script
    • Flex
    • Javascript & Libraries
    • Perl Programming
    • PHP/MySQL
    • Programming Techniques
    • Regular Expression
    • Social Media
  • Recent Visitors
  • Recent Articles
    • How to Protect your SWF (AS2 or AS3). Prevent SWF Decompile using SWF Protector
    • PHP – Search in an Array for Values Matching a Pattern – Regex, Wildcard
    • PHP – How to Get Browser Properties and Capabilities – get_browser()
    • CakePHP – Search for records between two dates inclusively
    • Notebook/Laptop hangs frequently/random (on Low Battery when AC power is connected)
    • Swap Values Without Temp or A Third Variable in PHP
    • CakePHP – Auth Login Redirect Problem with Plugins
    • Convert PHP array to Javascript array
    • CakePHP – Accessing a model in AppController or in any Controllers
    • Save Images from Flash – Actionscript 3, Filereference.save, JPGEncoder
  • Archives
    • September 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
  • Tags
      vertically center horizontally align dead center div amoeba ask credit card workbench decryption solutions kiosk questions horizontal center align vertical center align css trick solution css encryption as2 flash regex as3 Regular Expression action script mysql array CakePHP php

  • Donate

      If you find this site helpful, please donate to Amoeba Solution Kiosk. We will be using the money received for conducting technology awareness camps and seminars for school and college students in rural areas of Kerala, India.

  • Links
    • Amoeba Solutions
    • Blog Catalog
  • Home
  • Amoeba
  • ASK
  • Community
  • Source Codes
  • Contact Us

© Copyright Amoeba Solution Kiosk. All rights reserved.
An Amoeba Concept!

Back to Top