str_replace help needed.

Discussion in 'PHP' started by baris22, Jan 12, 2009.

  1. #1
    hello,

    i am trying to replace

    alt="Image"

    with

    alt="<?=$row['title'];?>

    (there is space at the beginning and at the end of them)

    how can i do this? i am tried different ways but it did not work.

    
    
    $fullpageweb = str_replace(' alt="Image" ', ' alt="<?=$row['title'];?>" ', $fullpageweb);
    
    
    PHP:
    thanks
     
    baris22, Jan 12, 2009 IP
  2. iDemonix

    iDemonix Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you already have the $row array, then do it like this;

    $fullpageweb = str_replace(' alt="Image" ', ' alt=' . $row['title'] . ' ', $fullpageweb);
    PHP:
    If you use single quotes ( ' ' ) then PHP will not parse the string and will just dump whatever is inside them, if you use double quotes ( " " ) then PHP WILL parse the string and swap variables etc. In your case the variable was inside single quotes so PHP did not look for variables etc to replace, I just took the variable out of the string.

    Dan
     
    iDemonix, Jan 12, 2009 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3

    Thank you for your help.

    instead of . $row['title'] . i have to use <?=$row['title'];?>

    is there anyway i can do that?

    thanks
     
    baris22, Jan 12, 2009 IP
  4. rldowling03

    rldowling03 Notable Member

    Messages:
    1,763
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    200
    #4
    Well the <? and ?> is usualess as it is already in a php function so is not necessary
     
    rldowling03, Jan 12, 2009 IP
  5. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #5
    the whole thing is already covered by <?php start tags

    So, you do not have to open <? anymore
     
    ads2help, Jan 12, 2009 IP
  6. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    i am still learning....

    i wanted to use this <?=$row['title'];?> thinking i could use it at the output. i did not realize i can change it at the code before output.

    now.. i can not use

    
    $fullpageweb = str_replace(' alt="Image" ', ' alt=' . $row['title'] . ' ', $fullpageweb);
    
    PHP:
    instead i need to use

    
    $fullpageweb = str_replace(' alt="Image" ', ' alt=" . $title . ", $fullpageweb);
    
    PHP:
    but it does not work either.

    thanks
     
    baris22, Jan 12, 2009 IP
  7. rldowling03

    rldowling03 Notable Member

    Messages:
    1,763
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    200
    #7
    $fullpageweb = str_replace(' alt="Image" ', ' alt=" '. $title .' " ', $fullpageweb);
    PHP:
    That should work
     
    rldowling03, Jan 12, 2009 IP
  8. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #8
    Yes that should work. You missed the double quotes "
     
    ads2help, Jan 12, 2009 IP
  9. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #9
    it did not work.

    i tried the same code with preg_replace and it did not work either.
     
    baris22, Jan 12, 2009 IP
  10. rldowling03

    rldowling03 Notable Member

    Messages:
    1,763
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    200
    #10
    This is how I would originally do it in my scripts:

    
    $fullpageweb = str_replace("alt=\"Image\"", "alt=\"$title\"", $fullpageweb);
    
    PHP:
    You can also try str_ireplace

    That replaces strings regardles of case
     
    rldowling03, Jan 12, 2009 IP