Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    str_replace lowercase problem

    Hi
    I have a problem with str_replace function. I am replacing some chars and spaces so that I can use them i.e. as id of div.

    For some reason one time it outputs like " alfaromeo" and the other time like it should be "AlfaRomeo" - any ideas ?

    Code:
    	$marki = array ("Alfa Romeo", "Audi");
    	$replacestr = array(' ', '(', ')', '/', '-');
    
    <? foreach ($marki as $marka) {
           echo '<a href="javascript:toggle("' . str_replace($replacestr, '', $marka) . '");">' . $marka . '</a>
    		<div id="' . str_replace($replacestr, '', $marka) . '" style="display: none" class="textli">';
    } 
    ?>
    Output:

    Code:
    <a href="javascript:toggle(" alfaromeo");">Alfa Romeo</a>
    <div id="AlfaRomeo" style="display: none" class="textli">

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Not with what you have posted here it doesn't. The code you have has an additional space preceding the string which doesn't exist, and str_replace will not cause a replacement of case. Combined with the additional set of <? and ?> tags (you should avoid using short_tags btw), tells me there is code between the first two variables you've declared and the foreach which is affecting the $marki result.
    I'm betting that JS code won't work properly either. The use of the double quotes within the double quotes indicates to HTML that you have completed an attribute, so those should be changed to escaped single quotes.

  • Users who have thanked Fou-Lu for this post:

    meemee (05-20-2011)


  •  

    Posting Permissions

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