SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Stuck with a str_replace function??

    Hi all,

    i am stuck with this:
    Code:
    function replaceIFrame($str) {
        $search = '[iframe-tag]1234abcd[/iframe-tag]';
        $replace = '
    <iframe src="http://www.mydomain.com/framefile/1234abcd" frameborder="0" width="650" height="450"/>';
        $return = str_replace($search,$replace,$str);
        return $return;
    
    }
    Where 1234abcd differ each time.
    stuck with
    - how to get the code between [iframe-tag]'s
    - add this $replace
    - and use $replace in str_replace

    any tips

  2. #2
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i found a way to get the value between iframe-tag

    Code:
    function replaceIFrame($str) {
        $var = extractBetweenDelimeters($str,'[iframe-tag]', '[/iframe-tag]');
        $replace = '
    <iframe src="http://www.mydomain.com/framefile/1234abcd" frameborder="0" width="650" height="450"/>';
        $return = str_replace($search,$replace,$str);
        return $var;
    }
    $str = '[iframe-tag]1234abcd[/iframe-tag]';
    
    if (!function_exists("stripos")) {
      function stripos($str,$needle) {
       return strpos(strtolower($str),strtolower($needle));
      }
    }
    function extractBetweenDelimeters($inputstr,$delimeterLeft,$delimeterRight) {
       $posLeft  = stripos($inputstr,$delimeterLeft)+strlen($delimeterLeft);
       $posRight = stripos($inputstr,$delimeterRight,$posLeft+1);
       return  substr($inputstr,$posLeft,$posRight-$posLeft);
    }
    echo replaceIFrame($str);

  3. #3
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i think i found a solution

  4. #4
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    A simpler solution would be using preg_replace:

    PHP Code:
    function replaceIFrame($string) {
        
    $replace '<iframe src="http://www.mydomain.com/framefile/$1" frameborder="0" width="650" height="450"/>';

        return 
    preg_replace('|\[iframe\-tag\]([a-z0-9]+)\[/iframe\-tag\]|Ui'$replace$string);
            } 


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
  •