473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Extracting URL from a link containing a specific string

I'd like to get address of certain links with desired text. Let's say I want to get links containing a word "BMW".

Expand|Select|Wrap|Line Numbers
  1. <a href="http://abc.com/xxxx">Dodge</a>
  2. <a href="http://abc.com/a123.php">Used BMW card</a>
  3. <a href="http://xyz.com/ferrari">Brand new Ferraris</a>
  4. <a href="http://xyz.com/vjklj"><img src="pic.jpg" />Luxurious BMWs</a>
  5.  
The link can only contain a text or a text and an image.

Is there any way how to do that without using regular expressions?
Aug 18 '11 #1

✓ answered by milesmajefski

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title>Find Links with BMW in them</title>
  3. </head>
  4. <body>
  5. <?php
  6.  
  7.   $my_links = array();
  8.   $example_links =
  9.    '<a href="http://abc.com/xxxx">Dodge</a> 
  10.     <a href="http://abc.com/a123.php">Used BMW card</a> 
  11.     <a href="http://xyz.com/ferrari">Brand new Ferraris</a> 
  12.     <a href="http://xyz.com/vjklj"><img src="pic.jpg" />Luxurious BMWs</a>';
  13.  
  14.   $my_pos = strpos($example_links, '<a');
  15.  
  16.   while ($my_pos !== False)
  17.   {    
  18.     $anchorBeg = strpos($example_links, '<a', $my_pos); 
  19.     $anchorEnd = strpos($example_links, '/a>', $anchorBeg) + 2;
  20.     $cut_length = $anchorEnd - $my_pos + 1;  
  21.     $myStr = substr( $example_links, $anchorBeg, $cut_length );
  22.     if (strpos($myStr, "BMW") !== False)
  23.  
  24.       $my_links[] = $myStr;
  25.  
  26.     $my_pos = strpos($example_links, '<a', $anchorEnd);
  27.   }//end while
  28.  
  29.   //ouput
  30.   $count = 0;
  31.   foreach( $my_links as $str )
  32.   {
  33.     echo "<br />Link #$count: ";
  34.     echo $str;
  35.     $count++;
  36.   }  
  37. ?>
  38.  
  39.  
  40. </body>
  41. </html>
  42.  

2 2219
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title>Find Links with BMW in them</title>
  3. </head>
  4. <body>
  5. <?php
  6.  
  7.   $my_links = array();
  8.   $example_links =
  9.    '<a href="http://abc.com/xxxx">Dodge</a> 
  10.     <a href="http://abc.com/a123.php">Used BMW card</a> 
  11.     <a href="http://xyz.com/ferrari">Brand new Ferraris</a> 
  12.     <a href="http://xyz.com/vjklj"><img src="pic.jpg" />Luxurious BMWs</a>';
  13.  
  14.   $my_pos = strpos($example_links, '<a');
  15.  
  16.   while ($my_pos !== False)
  17.   {    
  18.     $anchorBeg = strpos($example_links, '<a', $my_pos); 
  19.     $anchorEnd = strpos($example_links, '/a>', $anchorBeg) + 2;
  20.     $cut_length = $anchorEnd - $my_pos + 1;  
  21.     $myStr = substr( $example_links, $anchorBeg, $cut_length );
  22.     if (strpos($myStr, "BMW") !== False)
  23.  
  24.       $my_links[] = $myStr;
  25.  
  26.     $my_pos = strpos($example_links, '<a', $anchorEnd);
  27.   }//end while
  28.  
  29.   //ouput
  30.   $count = 0;
  31.   foreach( $my_links as $str )
  32.   {
  33.     echo "<br />Link #$count: ";
  34.     echo $str;
  35.     $count++;
  36.   }  
  37. ?>
  38.  
  39.  
  40. </body>
  41. </html>
  42.  
Aug 18 '11 #2
Thank you very much, I'm interested in parsing out the URLs only, so I'll probably do that the same way you did it with the A tags. That means finding >href="< and >"< and then use substr() function.
Aug 18 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: smarto59 | last post by:
I'm using xml as an index for p2p system in local network. Each peer accesses the xml file which is stored in the server through their web browsers. Until know the peers can only view the shared...
1
by: robboll | last post by:
Using MS Access 2003 I am looking for a function that will search the entire Tables Collection for a specific string in text or memo fields. For example if I enter "widget" it interrogates the...
17
by: devmentee | last post by:
Hello, I am trying to create a map/dictionary where the type of key is known ie std::string, but the value could be of any built in type. ie. int, double etc. (something along the lines of...
7
by: Tempo | last post by:
Hello. I am having a little trouble extracting text from a string. The string that I am dealing with is pasted below, and I want to extract the prices that are contained in the string below. Thanks...
9
by: incredible | last post by:
how to sort link list of string
2
by: Pete | last post by:
I need to create a single query (Not a SQL query) against a single table that counts the number of records in the table, where the single field "tmp" contains specific string values If the field...
8
by: oop | last post by:
Hi all I am completely new to perl programming and completely confused. I need to count the number of tests which passed specific criteria, the issue I am having is that all pass tests are...
10
by: Dan | last post by:
I have a number of strings that represents time. 1w 2d 3h 15m 2d 3h 15m 4h 30m 45m I want to extract the number parts of my strings into separate variables for Weeks, Days, Hours and...
7
by: artemetis | last post by:
Ok, I'm back again. I've got the following table. TableName = tblOriginal uid - self explanatory firstName - self explanatory lastName - self explanatory groupMaster - contains a numeric...
15
by: Chocolade | last post by:
public void logger_reader(string logger_reader) { string item = @"Last"; //*** Coloring all the text inside the richTextBox1 in Red ***\\ string ftext...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.