Asked  8 Months ago    Answers:  5   Viewed   24 times

Possible Duplicate:
Grabbing the href attribute of an A element

I need to parse all links of an HTML document that contain some word (it's always different).

Example:

<a href="/bla:bla">BLA</a>
<a href="/link:link">BLA</a>
<a href="/link:bla">BLA</a>

I only need the links with "href=/link: ...." what's the best way to go for it?

$html = "SOME HTLM ";
$dom = new DomDocument();
@$dom->loadHTML($html);
$urls = $dom->getElementsByTagName('a');
foreach ($urls as $url)
{
    echo "<br> {$url->getAttribute('href')} , {$url->getAttribute('title')}";
    echo "<hr><br>";
}

In this example all links are shown, I need specific links.

 Answers

34

By using a condition.

<?php 
$lookfor='/link:';

foreach ($urls as $url){
    if(substr($url->getAttribute('href'),0,strlen($lookfor))==$lookfor){
        echo "<br> ".$url->getAttribute('href')." , ".$url->getAttribute('title');
        echo "<hr><br>";
    }
}
?>
Wednesday, March 31, 2021
 
Gerardo
answered 8 Months ago
48

A tokenizer is not the same as a parser. The tokenizer just produces tokens and not in a tree format. For those who are actually looking to produce an AST for PHP similar to what ruby_parser does for Ruby, use the PHP-Parser project (https://github.com/nikic/PHP-Parser).

The PHP-Parser project also comes with a pretty printer that turns your AST back to PHP.

Wednesday, March 31, 2021
 
keyBeatz
answered 8 Months ago
48

To remove the line and print the output to standard out:

sed '/pattern to match/d' ./infile

To directly modify the file – does not work with BSD sed:

sed -i '/pattern to match/d' ./infile

Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed:

sed -i '' '/pattern to match/d' ./infile

To directly modify the file (and create a backup) – works with BSD and GNU sed:

sed -i.bak '/pattern to match/d' ./infile
Tuesday, June 1, 2021
 
NewPHP
answered 5 Months ago
34

I am surprised this wasn't mentioned yet: what about using Zend_Reflection of Zend Framework? This may come in handy especially if you work with a software built on Zend Framework like Magento.

See the Zend Framework Manual for some code examples and the API Documentation for the available methods.

There are different ways to do this:

  • Pass a file name to Zend_Reflection_File.
  • Pass an object to Zend_Reflection_Class.
  • Pass an object and a method name to Zend_Reflection_Method.
  • If you really only have the comment string at hand, you even could throw together the code for a small dummy class, save it to a temporary file and pass that file to Zend_Reflection_File.

Let's go for the simple case and assume you have an existing class you want to inspect.

The code would be like this (untested, please forgive me):

$method = new Zend_Reflection_Method($class, 'yourMethod');
$docblock = $method->getDocBlock();

if ($docBlock->hasTag('return')) {
    $tagReturn = $docBlock->getTag('return'); // $tagReturn is an instance of Zend_Reflection_Docblock_Tag_Return
    echo "Returns a: " . $tagReturn->getType() . "<br>";
    echo "Comment for return type: " . $tagReturn->getDescription();
}
Sunday, August 1, 2021
 
Heisenberg
answered 3 Months ago
45

Using PHP:

$url = $_SERVER["REQUEST_URI"];

$isItBlog = strpos($url, 'blog');
$isItNews = strpos($url, 'news');

if ($isItBlog!==false)
{
    //url contains 'blog'
}
if ($isItNews!==false)
{
    //url contains 'news'
}

http://www.php.net/manual/en/function.strpos.php

Friday, October 15, 2021
 
dougv
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share