1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

str_replace cauing weird character --> �

Discussion in 'PHP' started by timallard, Oct 7, 2009.

  1. #1
    Hello,

    I am using str_replace to change spaces into a % for a mysql query.
    I am having a weird error sometimes where it gives me a � character and do not know why. any ideas?

    I search crying babies and i get: crying�bies (error)
    I search crying mice and i get: crying mice (correct)

    this is my code:
    SEMrush
    //Takes spaces and adds wildcard
    $search = array(' ');
    $replace = array('%');
    $subject = $SearchText;
    $SearchText = str_replace($search, $replace, $subject);

    It only happens for certain words...

    Thank You,
    -Tim
     
    timallard, Oct 7, 2009 IP
    SEMrush
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this instead:

    $searchText = preg_replace('/\s+/u', '%', $searchText);
    PHP:
    Not sure if it'll actually work though.. Just a test.
     
    premiumscripts, Oct 7, 2009 IP
  3. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #3
    Hi premiumscripts,

    same results, hmmm
     
    timallard, Oct 7, 2009 IP
  4. w47w47

    w47w47 Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    are you sure that this are the full arrays?

    $search = array(' ');
    $replace = array('%');

    or are there some other strings in the array too? or is only this one in it?
     
    w47w47, Oct 8, 2009 IP
  5. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #5
    that is the only thing in the array thus far
     
    timallard, Oct 9, 2009 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Think this is a problem with the content type. try changing it in your meta
    <meta http-equiv="content-type" content="text/html;charset=ISO-8859-1">
     
    JAY6390, Oct 9, 2009 IP
  7. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #7
    ALMOST!!!! this is what happens now... "construction delays" turns into "constructionÞlays" are there any other options i could try? I have no idea why this is happening i don't thin i am doing anything different.
     
    timallard, Oct 9, 2009 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    you say this is coming from your database right? Do you have any special encoding on your DB charset?
     
    JAY6390, Oct 9, 2009 IP
  9. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #9
    this is what i have: squery varchar(255) latin1_swedish_ci (this was default)

    --- thanks for your help so far by the way!
     
    timallard, Oct 9, 2009 IP
  10. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #10
    can you do an echo of the exact query you are running? (not the result, ie echo $query; )
     
    JAY6390, Oct 9, 2009 IP
  11. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #11
    I am searching "construction delays" and my $Query is echoing "constructionÞlays"
     
    timallard, Oct 9, 2009 IP
  12. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #12
    can you paste the full code from the start of you modifying the search string to the mysql_query line please
     
    JAY6390, Oct 9, 2009 IP
    timallard likes this.
  13. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #13
    sure thing:


    $Query = $_POST['Query'];

    $query1= "SELECT * FROM curiosities WHERE Curiosity LIKE '"."%".mysql_real_escape_string($Query)."%"."' $sort";
    $result = mysql_query($query1) or die(mysql_error());
     
    timallard, Oct 9, 2009 IP
  14. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #14
    ok can you do this after the $query1 line
    echo $query1;
     
    JAY6390, Oct 9, 2009 IP
  15. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #15
    "SELECT * FROM curiosities WHERE Curiosity LIKE '%constructionÞlays%' ORDER BY cid ASC"
     
    timallard, Oct 9, 2009 IP
  16. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #16
    hmm, from the sounds of it the problem is lying in the sending of the data rather than the queries. Can you post the html for the form
     
    JAY6390, Oct 9, 2009 IP
  17. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #17
    <form id="Search" name="form1" method="post" action="searching.php">
    <label> <span class="style3">Search By: </span>
    <select name="SearchBy" class="" id="select" style="width:120px;">
    <option value="<?php echo $dropdownSearchByValue; ?>" selected="selected"><?php echo $dropdownSearchBy; ?></option>
    <option value="Category">Category</option>
    <option value="Date">Date</option>
    <option value="Keyword">Keyword</option>
    <option value="LastName">Last Name</option>
    <option value="Town">Town</option>
    <option value="ZipCode">Zip Code</option>
    </select>
    </label>
    &nbsp;
    <label>
    <span class="style3">Query:</span>
    <input name="SearchText" type="text" class="" id="textfield" value="<?php echo $Query; ?>" />
    </label>
    &nbsp; <?php echo $Query; ?>
    <label><span class="style3">Sort By:</span>
    <?php $dropdownSortBy = "Most Recent"; $dropdownSortByValue = "MostRecent"; ?>
    <select name="SortBy" class="" id="select1" style="width:150px;">
    <option value="<?php echo $dropdownSortByValue; ?>" selected="selected"><?php echo $dropdownSortBy; ?></option>
    <option value="LastName">Last Name</option>
    <option value="ZipCode">Zip Code</option>
    </select>
    </label>
    &nbsp;
    <label>
    <input type="hidden" value="<?php echo $dropdownSearchBy; ?>" name="dropdownSearchBy" />
    <input type="hidden" value="<?php echo number_format($num_rows[0]); ?>" name="QueryNum" />
    <input type="submit" name="button" id="button" value="Search" class="btn" />
    </label>
    <label>
    <!--<input type="text" id="datepicker">-->
    </label>
    </form>
     
    timallard, Oct 9, 2009 IP
  18. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #18
    hmm, I don't see an input that you are getting the $_POST['Query'] from in that code whatsoever
    You should have an
    <input type="text" id="Query" name="Query" />
    HTML:
    somewhere in there
     
    JAY6390, Oct 9, 2009 IP
  19. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #19
    $search = array(' ');
    $replace = array('%');

    Change the above array to this

    $search = array(" ");
    $replace = array("%");
     
    php-lover, Oct 10, 2009 IP
  20. w47w47

    w47w47 Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    JAY this is he's query input: <input name="SearchText" type="text" class="" id="textfield" value="<?php echo $Query; ?>" />

    timallard try to change this:

    //Takes spaces and adds wildcard
    $search = array(' ');
    $replace = array('%');
    $subject = $SearchText;
    $SearchText = str_replace($search, $replace, $subject);

    to:

    //Takes spaces and adds wildcard
    $SearchText = $_POST['SearchText'];
    $SearchText = str_replace(" ", "%", $SearchText);

    and BTW.. for what do you use/need the $subject variable for?
     
    w47w47, Oct 10, 2009 IP