Replace links in text php

Use php.net/domdocument — Everything else is crap, really. If you have a handful static links you want to replace, use strireplace. If more complex, parse the DOM.

2 Answers 2

$dom = new DOMDocument; $dom->loadHTML($html); foreach ($dom->getElementsByTagName('a') as $node) < //Do your processing here >

OK, since there’s no clear answer on how to manipulate the DOM in the way, I think, you want to manipulate it:

$foo = ' 

Some BS and Link!

'; $dom = new DOMDocument; $dom->loadHTML($foo);//parse the DOM here $links = $dom->getElementsByTagName('a');//get all links foreach($links as $link) nodeValue.': '.$link->getAttribute('href');//inner text: href attribute $replaceNode = $dom->createTextNode($replaceText);//create a DOMText instance $link->parentNode->replaceChild($replaceNode, $link);//replace the link with the DOMText instance > echo $dom->saveHTML();//echo the HTML after edits.
  

Some BS and Link!: https://www.google.com

Just Start by reading the DOMDocument manual, and click through to all methods (and related classes) that I’m using here. The DOMDocument API, like the DOM API in client-side JS, is bulky and not really that intuitive, but that’s just how it is.
Echoing the actual html,without the doctype can be done using the saveXML method, and/or some string operations. all in all, it shouldn’t be too difficult to get to where you want, using this code as a basis, and the links provided.

Читайте также:  Python получить имя функции внутри функции

Источник

I want to replace link to text «Title», but only from http://abc.com. But I don’t know how ( I tried Google ), can you explain for me. I’m not good in PHP. Thanks in advance.

2 Answers 2

Not sure I really understand what you’re asking, but if you :

  • Have a string that contains some HTML
  • and want to replace all links to abc.com by some text

Then, a good solution (better than regular expressions, should I say !) would be to use the DOM-related classes — especially, you can take a look at the DOMDocument class, and its loadHTML method.

For example, considering that the HTML portion is declared in a variable :

$html = some text

Title

some more text

Title

and some again

HTML;

You could then use something like this :

$dom = new DOMDocument(); $dom->loadHTML($html); $tags = $dom->getElementsByTagName('a'); for ($i = $tags->length - 1 ; $i > -1 ; $i--) < $tag = $tags->item($i); if ($tag->getAttribute('href') == 'http://abc.com') < $replacement = $dom->createTextNode($tag->nodeValue); $tag->parentNode->replaceChild($replacement, $tag); > > echo $dom->saveHTML(); 

And this would get you the following portion of HTML, as output :

  

some text

Title

some more text

Title

and some again

Note that the whole Title portion has been replaced by the text it contained.

If you want some other text instead, just use it where I used $tag->nodeValue , which is the current content of the node that’s being removed.

Unfortunately, yes, this generates a full HTML document, including the doctype declaration, and tags, .

Источник

i want replace (http://msn.com and http://google.com) and other links , but links of images like this (http://google/2013/06/geak-eye-mars.jpg) Remain as it is .. i hope u understand me .. i want only replace all links Between this tag

$text = ereg_replace("all links","another link">",$text); 

3 Answers 3

$text =  

bla bla bla bla bla bla

any word bla bla bla bla bla bla

LOD; $doc = new DOMDocument(); @$doc->loadHTML($text); $aNodes = $doc->getElementsByTagName("a"); foreach($aNodes as $aNode) < $href = $aNode->getAttribute("href"); $new_href = '. Youhou. '. $href; $aNode->setAttribute("href", $new_href); > $new_text = $doc->saveHTML();

thnx that’s worked good But there is a problem in the charset , Is it possible to be solved by iconv()

@o6qr: You must deal with the charset of your html document, code editor, wordpress configuration, server configuration, loadHTML, to solve the problem. Good luck! Perhaps your html document isn’t encoded with utf8, try to replace it with ISO8859-??

If you want a permanent solution that won’t break, use a DOM parser, like the other answers suggest. Using regular expressions to parse html is a really bad idea.

However, if you just want a one-time, quick solution, something like this will do:

preg_replace('/(href=["\']?)[^"\']+/', '$1' . $newtext, $html); 

Guaranteed, this will fail on some html files (like if the URL is not wrapped in quotes), but it will work with most as long as the person who wrote the html file used best practices. Don’t use this in production code. Ever.

Источник

In this tutorial, we will tackle about Replacing the Plain Text Links of a content with an Anchor Tag of HTML using PHP. We will be using the PHP’s preg_replace() function. For those who dont have any idea about the preg_replace, this function replace all the occurence of a string that matches the given pattern to your desired format or replacement string.

We will be creating a simple PHP Web App that is able to save content to the database. The simple app will automatically replace the plain text links from the content with a clickable link that can redirect users to the link’s page. I will be using Bootstrap and XAMPP as my local webserver to create and run the tutorial code.

Creating the Database

Create a new Database naming simple_db. Then, navigate to the SQL Tab of the database and ccopy/paste the SQL script below to create the contents table and insert the sample data.

Creating the Interface

The script below is the code for our simple application interface. This includes also the form of create the contents. Save this file as index.php.

Creating Database Connection

The code below is our database connection for saving and fetching the data on the database. Paste the code below in the beginning line of your index.php file.

Creating the Save Query

The code below is the PHP Script that saving or inserting the data on the database. Psete the code below the database connection sript.

Fetching Data from Database

The code below is the script that displays the list of contents from the database. This contains the main goal of this tutorial which is converting or replacing the plain text links with HTML anchor tag. Paste the code below inside the «unorederd list» tag on the index.php file.

That’s it. The simple web app is only written in a sinlge PHP file.

Here’s the Full Source Code.

That’s it! I hope this simple tutorial will help you with what you and for your future PHP Project. Explore more on this website for more Tutorials and Free Source Codes.

Tags

Источник

Оцените статью