Pick up word in the middle of a text

Asked

Viewed 539 times

4

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

This word will always change according to the file that is selected.

<a href="uploads/tutorial.pdf">tutorial.pdf</a>
  • 1

    The text of the description or the href?

  • It is no longer simple to get the 'description' of the javascript link?

  • href’s code returns directly from the database, so I just want to display the file name. If it is taken from "uploads/tutorial.pdf" or href description it is no problem.

  • Post your php code that inserts this html, so it’s easier to get an answer already embedded in what you need.

4 answers

3


You can use regex and move on to the preg_match() the pattern used in string formation

$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, he’s extracting the content pdf tutorial. from inside the property href

UPDATE

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

$link = 'uploads/tutorial.pdf';

if (preg_match("/\/(.*)\.pdf/s",$link, $results)){
    print_r($results[1]);
}
  • thank you very much! worked out the way I needed.

  • is worth giving a studied in regex, is very versatile and can be used in virtually all languages

2

Use the function split

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

$link = "uploads/tutorial.pdf";

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

print "$fileName[0] <br />"; // retorno "uploads"
print "$fileName[1] <br />" ;// retorno "tutorial.pdf"
  • split is deprecated and has been removed from php7, in which case exchange for explode

  • I made the change using the explode, but this returning me in Finame[1] omprovante.pdf">comprovante.pdf

  • I changed the delimiter to ">" is now bringing what I need.

  • I realized that this getting voucher.pdf </a , what I can do to go to the . pdf?

  • @If your purpose is to remove </a, use the str_replace $text = str_replace('</a','',$text)

1

You can use the function simplexml_load_string:

$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 function getElementsByTagName to select all elements with the tag a and get your 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

See DEMO

1

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

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

echo $fileName;

pdf tutorial.

  • $link = "<a href="uploads/tutorial.pdf">tutorial.pdf</a>";

  • As well as the code ?

  • it doesn’t just give me the path but the html code of the link already mounted

  • 1

    Haaaaa, so they already answered. You have to use regular expression.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.