scvinodkumar Posted August 6, 2009 Share Posted August 6, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/169078-shorten-the-texturl-between-tag/ Share on other sites More sharing options...
trq Posted August 6, 2009 Share Posted August 6, 2009 Your going to need to give us a clearer description than that I'm afraid. Quote Link to comment https://forums.phpfreaks.com/topic/169078-shorten-the-texturl-between-tag/#findComment-892062 Share on other sites More sharing options...
scvinodkumar Posted August 6, 2009 Author Share Posted August 6, 2009 for better understading replacing link as text. sorry for my bad english Quote Link to comment https://forums.phpfreaks.com/topic/169078-shorten-the-texturl-between-tag/#findComment-892065 Share on other sites More sharing options...
Adam Posted August 6, 2009 Share Posted August 6, 2009 preg_replace('#<a[^>]*>(.*?)</a>#', "$1", $str); Edit: I'd only read your last post, but I'm not sure that's exactly what you're after? Basically just leaves the text from a link and removes the rest. Quote Link to comment https://forums.phpfreaks.com/topic/169078-shorten-the-texturl-between-tag/#findComment-892088 Share on other sites More sharing options...
scvinodkumar Posted August 6, 2009 Author Share Posted August 6, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/169078-shorten-the-texturl-between-tag/#findComment-892101 Share on other sites More sharing options...
Adam Posted August 6, 2009 Share Posted August 6, 2009 Do you want to apply this to all links, or just certain ones (e.g. links that exceed a certain length)? Quote Link to comment https://forums.phpfreaks.com/topic/169078-shorten-the-texturl-between-tag/#findComment-892104 Share on other sites More sharing options...
scvinodkumar Posted August 6, 2009 Author Share Posted August 6, 2009 Can i have for both, ie when links exceed limit and for all the links. because in some situations i need to apply for all links and in some situation i need to apply when the limit exceed Quote Link to comment https://forums.phpfreaks.com/topic/169078-shorten-the-texturl-between-tag/#findComment-892106 Share on other sites More sharing options...
Adam Posted August 6, 2009 Share Posted August 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/169078-shorten-the-texturl-between-tag/#findComment-892120 Share on other sites More sharing options...
scvinodkumar Posted August 6, 2009 Author Share Posted August 6, 2009 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' Quote Link to comment https://forums.phpfreaks.com/topic/169078-shorten-the-texturl-between-tag/#findComment-892132 Share on other sites More sharing options...
Adam Posted August 6, 2009 Share Posted August 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/169078-shorten-the-texturl-between-tag/#findComment-892141 Share on other sites More sharing options...
scvinodkumar Posted August 6, 2009 Author Share Posted August 6, 2009 Many many thanks Adam to solve this issue Quote Link to comment https://forums.phpfreaks.com/topic/169078-shorten-the-texturl-between-tag/#findComment-892151 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.