SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help with str_replace

    I need help with function str_replace

    I want to convert news title stored on mysql and i am using str_replace.

    If i use this code for a direct conversion everything is okay (one string only):

    PHP Code:
    function convert($string) {
        
    $before = array('','','','','','','','','','','','','','','','','','','','','','','','','','',' ');
        
    $after = array('a','A','a','A','a','A','a','A','e','E','e','E','i','I','o','O','o','O','o','O','u','U','u','U','c','C','-');
        return 
    str_replace($before,$after,$string);
    }

    echo 
    convert('21345  '); 
    but, if i insert this code in a loop, only part of str_replace works. why?

    PHP Code:
    function convert($string) {
        
    $before = array('','','','','','','','','','','','','','','','','','','','','','','','','','',' ');
        
    $after = array('a','A','a','A','a','A','a','A','e','E','e','E','i','I','o','O','o','O','o','O','u','U','u','U','c','C','-');
        return 
    str_replace($before,$after,$string);
    }

    $value=900;

    // for($i = 0; $i < 33; $i = $i + 1) {

    $i 0;

    while (
    $i 6) {
        
    $sql mysql_query("select newsid, news_title from news where newsid='$value'");

        
    $row mysql_fetch_array($sql);

        
    $title strip_tags($row['news_title']);
        
        
    $title convert($title);

        echo 
    '<tr><td>' $row['newsid'] . '</td><td>' $row['news_title'] . '</td><td>' $title '</td></tr>';

        
    $i $i 1;
        
    $value $value 1;


    can anybody help me? thanks in advance!

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,153
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    What do you mean by "only part", what part? Are you sure it's not a database thing?

  3. #3
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ups! with what you said i think i found the problem.

    the problem is that text cames from mysql with html entities.

    i forgot about this and spent hours trying to understand.

    sorry for question and thanks.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    better way then using str_replace is to use strtr
    PHP Code:
    strtr$string'áÁãÃâÂàÀéÉêÊíÍóÓõÕôÔúÚüÜçÇ ''aAaAaAaAeEeEiIoOoOoOuUuUcC-' ); 
    Or use iconv + //TRANSLIT
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •