jumasol Posted September 27, 2013 Share Posted September 27, 2013 Hi. I am looking for some script to detect if there is a link with a certain word in an concrete page to insert css style in it. I have checked different proposals, but none of them offers a correct solution. Thanks for any reply. Quote Link to comment https://forums.phpfreaks.com/topic/282478-detecting-word-in-link-in-page/ Share on other sites More sharing options...
Ch0cu3r Posted September 27, 2013 Share Posted September 27, 2013 (edited) So you want to loop through all the links on a page and apply css to a specific word or to the whole link? To give a nudge in the right direction have a look at this example script for highlighting words in text <style type="text/css"> .hightlight { color: red; background-color: yellow; } </style> <?php // wraps word with span tag with hightlight class applied function hightlight_word($word) { return '<span class="hightlight">' . $word[0] . '</span>'; } // some dummy text $text = ' I like cats and dogs. cats like to each fish. dogs like to chase cats. '; // word to search for $word = 'cats'; // search for word and apply highlight_word() $text = preg_replace_callback("~\b$word\b~i", 'hightlight_word', $text); // display text with highlighted word echo nl2br($text); ?> Edited September 27, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282478-detecting-word-in-link-in-page/#findComment-1451417 Share on other sites More sharing options...
jumasol Posted September 27, 2013 Author Share Posted September 27, 2013 (edited) No. Just detect a link with a certain word (for instance "section/usa" in a link) in the html code of a site, and then insert a css file or style in the index.php to be applied to the whole site. The link (example = <link href="templates/section/usa.html" >USA</a>) is in the html code and the part ""section/usa" is what must be detected through php. So the script would be in the index.php, searching for such words in all the links in a given page within the website. Edited September 27, 2013 by jumasol Quote Link to comment https://forums.phpfreaks.com/topic/282478-detecting-word-in-link-in-page/#findComment-1451420 Share on other sites More sharing options...
Joshua F Posted September 28, 2013 Share Posted September 28, 2013 I think this might help you with what you're wanting. It might not, though. Quote Link to comment https://forums.phpfreaks.com/topic/282478-detecting-word-in-link-in-page/#findComment-1451494 Share on other sites More sharing options...
jumasol Posted October 2, 2013 Author Share Posted October 2, 2013 I have found this: <?php$ruta="http://www. sitio .com/index.php?option=com_xmap&view=xml&tmpl=component&id=1";$dom=new DOMDocument;$dom->preserveWhiteSpace= FALSE;$dom->load($ruta);$links=$dom->getElementsByTagName('descriptions');foreach($links as $link){echo '<link href="/css/xxxxxxxxxxxxxxxxxxx.css" rel="stylesheet" type="text/css" />';}?> But does not work for some reason. The sitemap link is correct and there is an hyperlink to "descriptions", but the script does not work. Some comment? Quote Link to comment https://forums.phpfreaks.com/topic/282478-detecting-word-in-link-in-page/#findComment-1452195 Share on other sites More sharing options...
Ch0cu3r Posted October 2, 2013 Share Posted October 2, 2013 sitio.com/index.php?option=com_xmap&view=xml&tmpl=component&id=1 returns XML data, not HTML. So the HTML you're echp'ing in the foreach loop wont do anything. Quote Link to comment https://forums.phpfreaks.com/topic/282478-detecting-word-in-link-in-page/#findComment-1452204 Share on other sites More sharing options...
jumasol Posted October 2, 2013 Author Share Posted October 2, 2013 This other option does not work either. And the url links to an html document: <?php$ruta="http://www. sitio .com/sitemap";$dom=new DOMDocument;$dom->preserveWhiteSpace= FALSE;$dom->load($ruta);$links=$dom->getElementsByTagName('descriptions');foreach($links as $link){echo '<link href="/css/xxxxxxxxxxxxxxxxxxx.css" rel="stylesheet" type="text/css" />';}?> Quote Link to comment https://forums.phpfreaks.com/topic/282478-detecting-word-in-link-in-page/#findComment-1452210 Share on other sites More sharing options...
Ch0cu3r Posted October 2, 2013 Share Posted October 2, 2013 Do you understand what $dom->getElementsByTagName('descriptions'); is doing? It is trying find tags such as <descriptions></descriptions>, these do not exist in HTML. What specific tag are you looking for, post example html code you're wanting to find and what you want it to do with it. Quote Link to comment https://forums.phpfreaks.com/topic/282478-detecting-word-in-link-in-page/#findComment-1452212 Share on other sites More sharing options...
jumasol Posted October 2, 2013 Author Share Posted October 2, 2013 No. What I am trying to detect link names in a sitemap. The sitemap might be an xml or html file. When a certain link with a given name is in the page, a css file would be included in the index. Thus, and in this case, www. sitio com/descriptions in the sitemap. Quote Link to comment https://forums.phpfreaks.com/topic/282478-detecting-word-in-link-in-page/#findComment-1452214 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.