SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Remove special characters from php result

    Hello all

    I have a CMS script, but I'm running into some issues

    When ever I create a post that contains $ - or . % and some other ones...
    on the front page the URL is displaying those characters is there any way to strip those characters?

    this is the code I'm currently using and it is only removing the "& "
    PHP Code:
    <?php echo str_ireplace("&","",str_ireplace(" ","-",html_entity_decode($row['title']))); ?>.html"><?php echo str_ireplace("-"," ",$row['title']); ?>
    any help ? thanks!

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,096
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Replace

    PHP Code:
    str_ireplace("&","", ... 
    with

    PHP Code:
    str_ireplace(array("&","-",".","%"),"", ... 
    (expand as needed)

    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much! that worked like a charm

    I have one more request however if you can help me out one more time

    It is similar problem I'm facing in this part of the script, as the one I mentioned above here is the code:

    PHP Code:
                    <?php    while($row=mysql_fetch_assoc($getNews)){    ?> 
                    

            
                    <div class="homeres">

                <div class="doscol">
                
                    <a href="http://www.xxxx.com/<?=str_ireplace(" ","-",html_entity_decode($row['genre']));?>/<?=str_ireplace(" ","-",html_entity_decode($row['name']));?>.html"><?=html_entity_decode($row['name']);?></a>

                            <?php    $i++;    }?>

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,096
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
                    <?php    while($row=mysql_fetch_assoc($getNews)){    ?> 
                    

            
                    <div class="homeres">

                <div class="doscol">
                
                    <a href="http://www.xxxx.com/<?=str_ireplace(array("&","-",".","%"," "),"-",html_entity_decode($row['genre']));?>/<?=str_ireplace(array("&","-",".","%"," "),"-",html_entity_decode($row['name']));?>.html"><?=html_entity_decode($row['name']);?></a>

                            <?php    $i++;    }?>
    Since the code you posted replaced spaces, I added that to the list of characters to be replaced as well
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried the above but what is happening now is it is removing "&" but it is placing a dash (-) where the & used to be and the link is not working

    is this somehow problem with my .htaccess ?

    thanks

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,096
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Sinz View Post
    I tried the above but what is happening now is it is removing "&" but it is placing a dash (-) where the & used to be and the link is not working
    I thought that is what you wanted? Please explain in more detail what you're trying to achieve
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The issue with the characters is that they are all links: so when you click on one it sends you to the actual news story etc..

    so when I tried the code you posted it is removing the & but now the link looks like this (example) "buy-roses-for-only--25-dollars " and when I click on it, won't go to the actual item
    I think the issue is the added - before the 25 or something conflicting with my htaccess?

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,096
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    So if I'm getting this correct you don't want to rewrite the & character?

    In that case the line would be
    PHP Code:
     <a href="http://www.xxxx.com/<?=str_ireplace(array("-",".","%"," "),"-",html_entity_decode($row['genre']));?>/<?=str_ireplace(array("-",".","%"," "),"-",html_entity_decode($row['name']));?>.html"><?=html_entity_decode($row['name']);?></a>
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    So if I'm getting this correct you don't want to rewrite the & character?

    In that case the line would be
    PHP Code:
     <a href="http://www.xxxx.com/<?=str_ireplace(array("-",".","%"," "),"-",html_entity_decode($row['genre']));?>/<?=str_ireplace(array("-",".","%"," "),"-",html_entity_decode($row['name']));?>.html"><?=html_entity_decode($row['name']);?></a>

    I tried that and it does remove the characters but now the URL is displayed like: http://www.xxxx.com/Free-shipping-and-20--off

    after the 20 it is placing and an extra -



    thanks


    (the original entry to the db is Free shipping and 20% off)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •