Escaping in PHP

Discussion in 'PHP' started by stevenswing, Jan 17, 2012.

  1. #1
    I'm having a little trouble running a urlencode on the following string within the str_replace function.

    The String:
      $bbcode_tpl['url1'] = str_replace('{URL}', '\\1', $bbcode_tpl['url']);
    Code (markup):
    Basically (not correct code - but what I want to do is this:

    $bbcode_tpl['url1'] = str_replace('{URL}', '\\1', urlencode($bbcode_tpl['url']));
    Code (markup):
    What's the correct syntax for that?

    Really appreciate any help.
     
    stevenswing, Jan 17, 2012 IP
  2. modz

    modz Well-Known Member

    Messages:
    95
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    103
    #2
    I guess that:
    $bbcode_tpl['url'] = 'http://blabla.com/bla.php?blabla=true'
    PHP:
    I think that you want to make this:
    $bbcode_tpl['url1'] = urlencode('http://blabla.com/')
    PHP:
    Am I right? If not, please tell us what exactly you want $bbcode_tpl['url1'] to look like, so we can help you.
     
    modz, Jan 17, 2012 IP
  3. stevenswing

    stevenswing Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for your reply.

    No, $bbcode_tpl['url'] is a long string and what we are doing it only replacing part of the string, however with the replaced part I want to URLENCODE it whilst doing the replace.

    Here is the full code so that it may help:

     // We do URLs in several different ways..
    		$bbcode_tpl['url'] = 'str_replace("\\\'","\'","'.str_replace('{NOFOLLOW}', '".add_nofollow(str_replace("\\\'","\'","\\1"))."', addslashes($bbcode_tpl['url'])).'")';
            $bbcode_tpl['url1'] = str_replace('{URL}', '\\1', $bbcode_tpl['url']);
            $bbcode_tpl['url1'] = str_replace('{DESCRIPTION}', '\\1', $bbcode_tpl['url1']);
    
            $bbcode_tpl['url2'] = str_replace('{URL}', 'http://\\1', $bbcode_tpl['url']);
            $bbcode_tpl['url2'] = str_replace('{DESCRIPTION}', '\\1', $bbcode_tpl['url2']);
    
            $bbcode_tpl['url3'] = str_replace('{URL}', '\\1', $bbcode_tpl['url']);
            $bbcode_tpl['url3'] = str_replace('{DESCRIPTION}', '\\2', $bbcode_tpl['url3']);
    
            $bbcode_tpl['url4'] = str_replace('{URL}', 'http://\\1', $bbcode_tpl['url']); 
            $bbcode_tpl['url4'] = str_replace('{DESCRIPTION}', '\\3', $bbcode_tpl['url4']);
    
            $bbcode_tpl['email'] = str_replace('{EMAIL}', '\\1', $bbcode_tpl['email']);
    Code (markup):
     
    stevenswing, Jan 17, 2012 IP
  4. modz

    modz Well-Known Member

    Messages:
    95
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    103
    #4
    Well, I guess that URL contains characters which can be affected by urlencode(). So you should first urlencode the URL and then use this:
    $bbcode_tpl['url1'] = str_replace("$urlencodedURL", '\\1', urlencode($bbcode_tpl['url']));
    PHP:
     
    modz, Jan 18, 2012 IP
  5. stevenswing

    stevenswing Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for the reply; we are getting this:

    You see... with this string:

    $bbcode_tpl['url1'] = str_replace('{URL}', '\\1', $bbcode_tpl['url']);
    Code (markup):
    {URL} is being replaced with "part" \\1 from the $bbcode_tpl['url'] - basically what I need to do is urlencode only the part that's replacing {URL}. Complex scenario and I'm not that sharp at coding.

    Any help or suggestions will be great!
     
    stevenswing, Jan 18, 2012 IP
  6. stevenswing

    stevenswing Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Any ideas guys?
     
    stevenswing, Jan 24, 2012 IP
  7. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #7
    You need to show us an example of:

    {URL} = ?
    $bbcode_tpl['url'] = ?

    Final result = ?
     
    Last edited: Jan 24, 2012
    MyVodaFone, Jan 24, 2012 IP
  8. stevenswing

    stevenswing Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ok, in simpler terms, how would I add an urlencode here:
            $bbcode_tpl['url1'] = str_replace('{URL}', '\\1', $bbcode_tpl['url']);
    
    Code (markup):
    For example, the correct syntax for doing something like this:
            $bbcode_tpl['url1'] = str_replace('{URL}', 'urlencode(\\1)', $bbcode_tpl['url']);
    Code (markup):
     
    stevenswing, Jan 24, 2012 IP
  9. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #9
    This {URL} has be part of this $bbcode_tpl['url'] and when it does match, this {URL} will be replace by this \\1

    So without examples of the two urls its going to be impossible to advise....

    Start at the beginning and explain exactly what your trying to do with the urls, you might not need urlencode() or you should be using urldecode() first, preg_replace() is always an option too.
     
    Last edited: Jan 24, 2012
    MyVodaFone, Jan 24, 2012 IP
  10. stevenswing

    stevenswing Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Is it not possible to urlencode it while it is being replaced? In the same action?

    For example, the line before does some strange things all in one go:
    		$bbcode_tpl['url'] = 'str_replace("\\\'","\'","'.str_replace('{NOFOLLOW}', '".add_nofollow(str_replace("\\\'","\'","\\1"))."', addslashes($bbcode_tpl['url'])).'")';
    Code (markup):
     
    stevenswing, Jan 24, 2012 IP
  11. stevenswing

    stevenswing Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Anyone have any ideas on this?
     
    stevenswing, Feb 2, 2012 IP