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?

Link to comment
Share on other sites

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

 

?>

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

 

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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