Jump to content

Recommended Posts

Hi i have a content which comes from database.

 

For example,

<a href="http://www.autobloggreen.com/2009/08/01/report-nissan-qazana-and-nuvu-could-join-ev-lineup/">http://www.autobloggreen.com/2009/08/01/report-nissan-qazana-and-nuvu-could-join-ev-lineup</a>
<p>Filed under: <a href="http://www.autobloggreen.com/category/ev-plug-in/" rel="tag">EV/Plug-in</a>, <a href="http://www.autobloggreen.com/category/nissan/" rel="tag">Nissan</a></p>
<a href="http://www.autoblog.com/gallery/geneva-2009-nissan-qazana-1"><img src="http://www.blogcdn.com/www.autobloggreen.com/media/2009/07/01-nissan-qazana-630.jpg" alt="" vspace="4" border="1" hspace="4"></a><br>
<div style="text-align: center;"><em><strong>Nissan Qazana Concept - click above for a high-res image gallery</strong></em></div>
http://www.autobloggreen.com/2009/08/01/report-nissan-qazana-and-nuvu-could-join-ev-lineup

 

I want to replace the link

 

<a href="link">text</a> as <a href="link">View link</a> and link as <a href="link">View link</a>

 

For example,

suppose, if the link come with a tag
<a href="http://www.autobloggreen.com/2009/08/01/report-nissan-qazana-and-nuvu-could-join-ev-lineup">http://www.autobloggreen.com/2009/08/01/report-nissan-qazana-and-nuvu-could-join-ev-lineup</a> 
as <a href="http://www.autobloggreen.com/2009/08/01/report-nissan-qazana-and-nuvu-could-join-ev-lineup">View link</a> 

if the link comes without a tag                                
and http://www.autobloggreen.com/2009/08/01/report-nissan-qazana-and-nuvu-could-join-ev-lineup as <a href="http://www.autobloggreen.com/2009/08/01/report-nissan-qazana-and-nuvu-could-join-ev-lineup">View link</a>

 

I am using the below code currently,

 

$text = preg_replace("/(https?://[^ ) !]+)/eim", "'<a href="\1" title="\1">view link</a>'", stripslashes($fetch->body));
if(strlen(strip_tags($text,'<a></a>'))>200)
   $description = substr(strip_tags($text,'<a></a>'),0,200).'  <a href="link">more...</a>';
else
     $description = strip_tags($text,'<a></a>');

 

How to do this using preg_replace?

 

Please help me

 

Link to comment
https://forums.phpfreaks.com/topic/169078-shorten-the-texturl-between-tag/
Share on other sites

I think i have not explained clearly.

 

As u see the content, it has some long links, for example,

 

<a href="http://www.autobloggreen.com/2009/08/01/report-nissan-qazana-and-nuvu-could-join-ev-lineup">http://www.autobloggreen.com/2009/08/01/report-nissan-qazana-and-nuvu-could-join-ev-lineup</a> 

 

i want to display this link as View link in a short manner.

 

and also there will be link like this

 

http://www.autobloggreen.com/2009/08/01/report-nissan-qazana-and-nuvu-could-join-ev-lineup

 

i also want to display this link "view link".

 

I hope you understand my needs now

 

 

 

 

 

To replace all:

 

preg_replace('#(<a[^>]*>)[^<]*(</a>)#', "$1View Link$2", $str);

 

To replace links exceeding 20 characters (in the example):

 

if (preg_match_all('#(<a[^>]*>)(.*?)(</a>)#', $str, $matches))
{
    foreach ($matches[2] as $key => $match)
    {
        if (strlen($match) > 20)
        {
            $str = str_replace($matches[1][$key] . $matches[2][$key] . $matches[3][$key], $matches[1][$key] . 'View Link' . $matches[3][$key], $str);
        }
    }
}

 

Uhh, it's pretty ugly, but works.

Great!!! Its working.

 

If u don't mind could you please tell me for this example,

 

http://google.com <br> http://www.google.com <br> www.google.com <br>
<a href='http://google.com'>http://google.com</a>

 

replace all links to 'view link'

 

 

 

 

Apply this before either of the other two:

 

$str = preg_replace('#(?<![">])\b(??:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&@\#/%=~_|$?!:,.]*[A-Z0-9+&@\#/%=~_|$]#', '<a href="\0">\0</a>', $str);

 

Weren't going to write my own version of matching URLs so borrowed this.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.