I want to get "/contact/new" from <a href="/contact/new">Contact us</a>
. The condition would be like that if a link has 'Contact' or 'Contact us' text then get the href value. There will be no class.
How can I do this?
I want to get "/contact/new" from <a href="/contact/new">Contact us</a>
. The condition would be like that if a link has 'Contact' or 'Contact us' text then get the href value. There will be no class.
How can I do this?
Using regex
and PHP
:
$text = '<a href="/contact/new">Contact us</a>';
preg_match_all('(<a href="([^"]*)">[Contact us|Contact]*</a>)', $text, $matches);
foreach ($matches[1] as $href) {
// Do whatever you want with the href attribute
echo $href;
}
Using jQuery
:
Select all a
elements, check if their html()
is the text that you are looking for return attr.("href")
$("a").each(function(index, element) {
if ($(elem).html() == "Contact" || $(elem).html() == "Contact us") {
// Do whatever you want with the href attribute
console.log($(elem).attr("href"));
}
});
I have solved by this piece of code. Obviously after getting approach from @Matias Cerrotta
foreach($dom->find('a') as $element) {
echo $element->plaintext . '<br>';
}
This can be accomplished using SimpleXML and XPath.
You will need to adjust how you load the page in to SimpleXML using file_get_contents
or some other method to read the page to a variable and then pass it through.
I have created a mock up that works below
<?php
$html = '
<a href="/contact/new">Contact us</a>
';
//Replace with your loading logic here
$xml = simplexml_load_string($html);
//Perform the search
$search = $xml->xpath('//a[contains(text(), "Contact us") or contains(text(), "Contact")]');
//Check the results have at least one value
if(count($search) !== 0 && $search !== false)
{
//Get first item
$item = $search[0];
//Get item attributes
$attributes = $item->attributes();
//Output the HREF attribute (need an existence check here (isset))
echo $attributes['href'];
}
The XPath method returns an array of the matches which will need to be filtered through if more than one result is returned, in the sample I am grabbing the first one and outputting the href attribute of the node.
The search finds all a
tags regardless of position in the string/document and checks that it contains either "Contact us" or "Contact".
Note: XPath is case sensitive and whilst there are ways to make it insensitive you will need to implement this yourself or write more conditions to check for.
If you need case insensitivity then check another Stack question, it has been covered before: