Yves Posted January 11, 2008 Share Posted January 11, 2008 Hi, I have an free articles directory. Webmasters can freely reprint articles for their website, ezine or newsletter. Each article has a Copy & Publish Article page with both HTML and plain text versions of the article. Each article contains an author resourse box with up to 3 links linking to the webpages the authors whats to rank for in the searchengines. Now, this author resourse box is just plain text in the mysql. See example below. First I would like to add I did about 10 different searches to spair you guys a new topic to no avail. I hope you're kind enough to help me out. Thanks. What I like to achieve is making strings like these Gregg Hall is an author living with his 18 year old son in Jensen Beach, Florida. Find more about Xbox games as well as <a href="http://www.nsearch.com/?k=Computers">Video Game Systems</a> at <a href="http://www.nsearch.com">http://www.nsearch.com</a> strings like these Gregg Hall is an author living with his 18 year old son in Jensen Beach, Florida. Find more about Xbox games as well as [http://www.nsearch.com/?k=Computers]Video Game Systems at [http://www.nsearch.com] As you can see I would like html 'href' links wrapped in [ ]. But if html 'href' links are the same as the display text, don't display any text behing the [] (see 2nd link in quote above) EDIT: $html is this plain text string Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/ Share on other sites More sharing options...
Ken2k7 Posted January 11, 2008 Share Posted January 11, 2008 Try: <?php $html = preg_replace("/<a href=(.*)>\w+<\/a>/i","[url=\\1]",$html); ?> Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436403 Share on other sites More sharing options...
Yves Posted January 11, 2008 Author Share Posted January 11, 2008 That didn't fully do it. It outputted this: Gregg Hall is an author living with his 18 year old son in Jensen Beach, Florida. Find more about Xbox games as well as Video Game Systems at http://www.nsearch.com There are not brackets and the anchored link wasn't transformed. PS: I edited my 1st code quote in 1st post to show that href's can contain any kind of link. Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436456 Share on other sites More sharing options...
Yves Posted January 11, 2008 Author Share Posted January 11, 2008 I found a great tutorial on preg_replace. At least I thought I did. And came up with this: <?php $urlfind = '/(<a href=")(.*)(">)(.*)(<\/a>)/'; $urlreplace = '[${2}]${4}'; $html = preg_replace($urlfind, $urlreplace, $html); ?> But it didn't work either. I'm not getting there. :/ Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436625 Share on other sites More sharing options...
cooldude832 Posted January 11, 2008 Share Posted January 11, 2008 just wondering if this gets you any where try t his for me <?php $file = "http://www.testsite.com"; $data = file_get_contents($file); if(!empty($data){ $data = strip_tags($data,"<a>"); } $data = str_replace("\n","",$data); $data = str_replace("</a>","\n",$data); $data = explode("\n",$data); print_r($data); ?> see what it says for you just curious Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436627 Share on other sites More sharing options...
Yves Posted January 11, 2008 Author Share Posted January 11, 2008 hm. Parse error: syntax error, unexpected '{' in /.../test.php on line 12 line 12 if(!empty($data){ Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436636 Share on other sites More sharing options...
cooldude832 Posted January 11, 2008 Share Posted January 11, 2008 missed a ) <?php $file = "http://www.testsite.com"; $data = file_get_contents($file); if(!empty($data)){ $data = strip_tags($data,"<a>"); } $data = str_replace("\n","",$data); $data = str_replace("</a>","\n",$data); $data = explode("\n",$data); print_r($data); ?> Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436638 Share on other sites More sharing options...
Yves Posted January 11, 2008 Author Share Posted January 11, 2008 I see. Well, it says: Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /.../test.php on line 11 Warning: file_get_contents(http://www.testsite.com) [function.file-get-contents]: failed to open stream: Connection timed out in /.../test.php on line 11 Array ( [0] => ) Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436644 Share on other sites More sharing options...
cooldude832 Posted January 11, 2008 Share Posted January 11, 2008 well u gotta put your page's name in there dauh Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436650 Share on other sites More sharing options...
papaface Posted January 11, 2008 Share Posted January 11, 2008 well u gotta put your page's name in there dauh lmao, sorry couldn't resist. Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436654 Share on other sites More sharing options...
Yves Posted January 11, 2008 Author Share Posted January 11, 2008 What if it now has a name and it still shows me the same errors. in fact, what's whith the whole test? Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436660 Share on other sites More sharing options...
Yves Posted January 11, 2008 Author Share Posted January 11, 2008 I see. Nerd joke. Get out more often. I think there is no reason for me, nor anyone to stick around here any longer. Thank you Ken2k7 for your kind help. I appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436665 Share on other sites More sharing options...
papaface Posted January 11, 2008 Share Posted January 11, 2008 I see. Nerd joke. Get out more often. I think there is no reason for me, nor anyone to stick around here any longer. Thank you Ken2k7 for your kind help. I appreciate it. It was hardly a joke, and even if it was, the joke was on you anyway. People are here to help, not dish out code. So when someone provides you the code the least you can do is actually look at it and try to figure out what it is actually doing! Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436693 Share on other sites More sharing options...
Yves Posted January 11, 2008 Author Share Posted January 11, 2008 I did look at it. I just don't know what 'file_get_contents' is. Because you laughed, I thought it a command to get information about the data, which is the testsite.com, a site I donnot own. So I though it could not get that information because I'm not the owner. ~ hence a joke pulled on me. I first didn't think someone with so many post would do that. But if that is the case, well then, I think its obvious for me to leave. Don't expect people who ask question to allready know as much as you do. And don't assume they don't look at the code. Believe me; those that don't look at the code must be less then 1% of visitors here. Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436697 Share on other sites More sharing options...
Yves Posted January 11, 2008 Author Share Posted January 11, 2008 argh - wrong foot - i hate it Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436705 Share on other sites More sharing options...
Ken2k7 Posted January 11, 2008 Share Posted January 11, 2008 Yves, I think I did my preg_replace given that there aren't any quotes in the <a> tag <a href="test.php">test</a> OR <a href='test.php'>test</a> It gets complicated when those quotes get in the way. Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436713 Share on other sites More sharing options...
papaface Posted January 11, 2008 Share Posted January 11, 2008 I did look at it. I just don't know what 'file_get_contents' is. Because you laughed, I thought it a command to get information about the data, which is the testsite.com, a site I donnot own. So I though it could not get that information because I'm not the owner. ~ hence a joke pulled on me. I first didn't think someone with so many post would do that. But if that is the case, well then, I think its obvious for me to leave. Don't expect people who ask question to allready know as much as you do. And don't assume they don't look at the code. Believe me; those that don't look at the code must be less then 1% of visitors here. Maybe you're a bit to sensitive. I'm sure anyone else would have laughed when they realised the simple error they made. You're just going a bit OTT. Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436715 Share on other sites More sharing options...
Yves Posted January 11, 2008 Author Share Posted January 11, 2008 My appologies. Ken: there are quotes. but they're always double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436727 Share on other sites More sharing options...
Ken2k7 Posted January 11, 2008 Share Posted January 11, 2008 Please try: <?php $html = preg_replace("/<a href=\"{1}(.+)?\">\w+<\/a>/","[url=\\1]",$html); ?> Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436760 Share on other sites More sharing options...
Yves Posted January 11, 2008 Author Share Posted January 11, 2008 that worked It displays the full url. what if I don't want to lose the anchor text. example: change this <a href="http://www.digital-cameras.com">Digital Cameras</a> to this [http://www.digital-cameras.com]Digital Cameras Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436775 Share on other sites More sharing options...
Ken2k7 Posted January 11, 2008 Share Posted January 11, 2008 that worked It displays the full url. what if I don't want to lose the anchor text. example: change this <a href="http://www.digital-cameras.com">Digital Cameras</a> to this [http://www.digital-cameras.com]Digital Cameras $html = preg_replace("/<a href=\"{1}(.+)?\">(\w+)<\/a>/","[url=\\1]\\2",$html); Quote Link to comment https://forums.phpfreaks.com/topic/85509-get-url-out-of-plain-text/#findComment-436823 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.