Pick word in the middle of a text

4

I need to get the word tutorial.pdf that is in the middle of the link.

This word will always change according to the file you select.

<a href="uploads/tutorial.pdf">tutorial.pdf</a>
    
asked by anonymous 19.09.2016 / 16:03

4 answers

3

You can use regex and pass to preg_match() the pattern used to form the string

$input = '<a href="uploads/tutorial.pdf">tutorial.pdf</a>';

if (preg_match("/<a href=\"uploads\/(.*)\">/s",$input, $results)){
    print_r($results[1]);
}

In this example, it is extracting tutorial.pdf content from within the href

UPDATE

As you said, the content of href is coming from the bank, you can use regex to extract the text between / and .pdf

$link = 'uploads/tutorial.pdf';

if (preg_match("/\/(.*)\.pdf/s",$link, $results)){
    print_r($results[1]);
}
    
19.09.2016 / 18:44
2

Use the split

This function returns an array of strings from a delimiter, in which case our delimiter is / .

$link = "uploads/tutorial.pdf";

$fileName = split ("/", $link);

print "$fileName[0] <br />"; // retorno "uploads"
print "$fileName[1] <br />" ;// retorno "tutorial.pdf"
    
19.09.2016 / 16:25
1

You can also use the combination of split() with end() :

$link       = "uploads/tutorial.pdf";
$file       = split ("/", $link);
$fileName   = end($file);

echo $fileName;
  

tutorial.pdf

    
19.09.2016 / 19:18
1

You can use the simplexml_load_string function:

$html = '<a href="uploads/tutorial1.pdf">tutorial1.pdf</a>';

$xml = simplexml_load_string($html);

echo $xml;         // tutorial1.pdf  
echo $xml['href']; // uploads/tutorial1.pdf

See DEMO

Another alternative is the class DOMDocument , you can use the getElementsByTagName to select all elements with the a tag and get their values:

$html = '<a href="uploads/tutorial1.pdf">tutorial1.pdf</a>
         <a href="uploads/tutorial2.pdf">tutorial1.pdf</a>
         <a href="uploads/tutorial3.pdf">tutorial1.pdf</a>';

$dom = new DOMDocument();
$dom->loadHTML($html);

$tags = $dom->getElementsByTagName('a');

foreach ($tags as $tag) {
       echo $tag->nodeValue . ": " . $tag->getAttribute('href') . "\n";
}

// tutorial1.pdf: uploads/tutorial1.pdf
// tutorial1.pdf: uploads/tutorial2.pdf
// tutorial1.pdf: uploads/tutorial3.pdf

View DEMO

    
19.09.2016 / 17:39