Jump to content

I need help with converting HREF link to lowercase and dashes


belick

Recommended Posts

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?

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>";

 

?>

 

 

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.

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

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.

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>

 

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!)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.