I'd like to get address of certain links with desired text. Let's say I want to get links containing a word "BMW". - <a href="http://abc.com/xxxx">Dodge</a>
-
<a href="http://abc.com/a123.php">Used BMW card</a>
-
<a href="http://xyz.com/ferrari">Brand new Ferraris</a>
-
<a href="http://xyz.com/vjklj"><img src="pic.jpg" />Luxurious BMWs</a>
-
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?
-
<html>
-
<head><title>Find Links with BMW in them</title>
-
</head>
-
<body>
-
<?php
-
-
$my_links = array();
-
$example_links =
-
'<a href="http://abc.com/xxxx">Dodge</a>
-
<a href="http://abc.com/a123.php">Used BMW card</a>
-
<a href="http://xyz.com/ferrari">Brand new Ferraris</a>
-
<a href="http://xyz.com/vjklj"><img src="pic.jpg" />Luxurious BMWs</a>';
-
-
$my_pos = strpos($example_links, '<a');
-
-
while ($my_pos !== False)
-
{
-
$anchorBeg = strpos($example_links, '<a', $my_pos);
-
$anchorEnd = strpos($example_links, '/a>', $anchorBeg) + 2;
-
$cut_length = $anchorEnd - $my_pos + 1;
-
$myStr = substr( $example_links, $anchorBeg, $cut_length );
-
if (strpos($myStr, "BMW") !== False)
-
-
$my_links[] = $myStr;
-
-
$my_pos = strpos($example_links, '<a', $anchorEnd);
-
}//end while
-
-
//ouput
-
$count = 0;
-
foreach( $my_links as $str )
-
{
-
echo "<br />Link #$count: ";
-
echo $str;
-
$count++;
-
}
-
?>
-
-
-
</body>
-
</html>
-
2 2219 -
<html>
-
<head><title>Find Links with BMW in them</title>
-
</head>
-
<body>
-
<?php
-
-
$my_links = array();
-
$example_links =
-
'<a href="http://abc.com/xxxx">Dodge</a>
-
<a href="http://abc.com/a123.php">Used BMW card</a>
-
<a href="http://xyz.com/ferrari">Brand new Ferraris</a>
-
<a href="http://xyz.com/vjklj"><img src="pic.jpg" />Luxurious BMWs</a>';
-
-
$my_pos = strpos($example_links, '<a');
-
-
while ($my_pos !== False)
-
{
-
$anchorBeg = strpos($example_links, '<a', $my_pos);
-
$anchorEnd = strpos($example_links, '/a>', $anchorBeg) + 2;
-
$cut_length = $anchorEnd - $my_pos + 1;
-
$myStr = substr( $example_links, $anchorBeg, $cut_length );
-
if (strpos($myStr, "BMW") !== False)
-
-
$my_links[] = $myStr;
-
-
$my_pos = strpos($example_links, '<a', $anchorEnd);
-
}//end while
-
-
//ouput
-
$count = 0;
-
foreach( $my_links as $str )
-
{
-
echo "<br />Link #$count: ";
-
echo $str;
-
$count++;
-
}
-
?>
-
-
-
</body>
-
</html>
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
by: incredible |
last post by:
how to sort link list of string
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |