belick Posted June 10, 2009 Share Posted June 10, 2009 I want to look for anywhere in the page that there is links and if there is a link with spaces like below then to convert it to: 1. Lowercase 2. spaces = deshes 3. add .html at the end href="Search Engine Optimization" will be: href="search-engine-optimization.html" is it possible? Quote Link to comment Share on other sites More sharing options...
ldougherty Posted June 10, 2009 Share Posted June 10, 2009 Hope this helps.. <?php $string = 'href="Search Engine Optimization"'; echo "String Before: $string<br><br>"; $string = strtolower($string); $pattern = '/ /'; $replacement = '-'; $string = preg_replace($pattern, $replacement, $string); $string = substr_replace($string,'.html"',-1); echo "String After: $string<br>"; ?> Quote Link to comment Share on other sites More sharing options...
Stooney Posted June 11, 2009 Share Posted June 11, 2009 For upper to lower case: toLowerCase() For space to dash: var.replace(' ', '-'); To add .html to end: var=var+'.html'; Quote Link to comment Share on other sites More sharing options...
belick Posted June 11, 2009 Author Share Posted June 11, 2009 Thanks, but the problem is that the preg need to look for any string and not for specific one... Quote Link to comment Share on other sites More sharing options...
Stooney Posted June 12, 2009 Share Posted June 12, 2009 If you use jquery you could do something along the lines of this (untested): $("a").each(function(){ $(this).attr('href', $(this).attr('href').toLowerCaser().replace(' ', '-')+'.html'); }) Again it's untested and just an idea out of my head, but I think it should work or at least be close. Can anyone confirm this? I am unable to test right now. Quote Link to comment Share on other sites More sharing options...
belick Posted June 12, 2009 Author Share Posted June 12, 2009 I was looking for smthing like this: echo preg_replace('~href="([^"])+"~', 'href="'.strtolower("$1").'"', $WebPage); but the strtolower("$1") is not working... also I want to look for anything without the html the idea is to look for any link like: Home Page, About Us and such and convert it to lowercase without spaces... and then to add .html Quote Link to comment Share on other sites More sharing options...
jacksonmj Posted June 13, 2009 Share Posted June 13, 2009 Use preg_replace_callback. For example: function link_replace_callback($matches) { $linkUrl = str_replace(" ","-",strtolower($matches[0])); return 'href="'.$linkUrl.'"'; } echo preg_replace_callback( '~href="([^"])+"~', "link_replace_callback", $WebPage); You could probably also use the callback function to leave intact anything with .html in it. Quote Link to comment Share on other sites More sharing options...
belick Posted June 14, 2009 Author Share Posted June 14, 2009 yes, thanks but 1 issue... I tried to add more text to return $linkUrl; and it always bring the extra text after the quotes: function link_replace_callback($matches) { $linkUrl = str_replace(" ","-",strtolower($matches[0])); return $linkUrl.".html"; } echo preg_replace_callback('~href="([^"])+"~', "link_replace_callback", $WebPage); and on my links I get: <a href="test-trest".html</a> Quote Link to comment Share on other sites More sharing options...
jacksonmj Posted June 14, 2009 Share Posted June 14, 2009 Sorry, should have used $matches[1]. The regexp also needed slight modification, and I've added code to leave hrefs ending in .html intact. function link_replace_callback($matches) { if (substr($matches[1],-5)==".html") return 'href="'.$matches[1].'"'; $linkUrl = str_replace(" ","-",strtolower($matches[1])); return 'href="'.$linkUrl.'.html"'; } echo preg_replace_callback('~href="([^"]+)"~',"link_replace_callback",$WebPage); (I've actually tested my code this time!) Quote Link to comment 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.