Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    90
    Thanks
    5
    Thanked 0 Times in 0 Posts

    str_replace problem

    Hi,

    I am trying to modify a code in my blog to replace a part of the text so it will insert a poll

    The original blog code works like this:

    PHP Code:
    $message str_replace(":!:""<img src=\"images/smilies/icon_exclaim.gif\" alt=\"\"></img>"$message); 
    I have tried to modify, but have only got this far:

    PHP Code:

    // Insert Polls

    $message str_replace("[poll1]""<iframe width=\"100%\" src=\"blog-poll.php?pollid=$id\" scrolling=\"no\" id=\"the_iframe\" onLoad=\"calcHeight();\" height=\"1px\" frameborder=\"0\" ></iframe>"$message); 
    where [poll1] should be found within $message so it will change the text to show the poll and the $id should be the number of the poll e.g. 1 if I type [poll1]

    I have found suitable regex "/^(.*\[poll)(.*)(\])/" but not sure how to implement this?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    I'm sort of betting that I'm not understanding this.

    Either you already know what the number is ... the variable $id as you've described ... and you do this ....
    PHP Code:
    // Insert Polls 

    $rString="[poll".$id."]";
    $message str_replace($rString"<iframe width=\"100%\" src=\"blog-poll.php?pollid=$id\" scrolling=\"no\" id=\"the_iframe\" onLoad=\"calcHeight();\" height=\"1px\" frameborder=\"0\" ></iframe>"$message); 
    Or you don't know what number is in the string [poll1] ... in this case it's 1, but it could be any number.
    Because you're using the variable called $id, I'm not really sure what your question really is.



    .

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    90
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hi mlseim,

    Sorry I will try and explain better,

    When writing a blog on my site, specific text changes through regex to show such a smiley image when : ) is typed.

    I wanted to type [poll1] and this would be replaced with:

    PHP Code:
    <iframe width="100%\" src=\"blog-poll.php?pollid=$id\" scrolling=\"no\" id=\"the_iframe\" onLoad=\"calcHeight();\" height=\"1px\" frameborder=\"0\" ></iframe> 
    The blog post is contained as a string within $message, I wanted to use regex to replace that part of the string where [poll1] is found or what ever number is within the brackets where poll and the enclosed brackets are found to change with the above.

    At the same time the number after poll would be assigned to the variable $id to display the correct poll from blog-poll.php

    The above method maybe long winded, but this is what I was trying to explain in the previous post.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Pattern matching is pretty much the way to go on this.
    PHP Code:
    // Insert Polls
    $message "This is my message.  It has a [poll171] in it.";

    $p '/\[poll(\d+)\]/i';
    $r '<iframe width=\"100%\" src=\"blog-poll.php?pollid=$1\" scrolling=\"no\" id=\"the_iframe\" onLoad=\"calcHeight();\" height=\"1px\" frameborder=\"0\" ></iframe>';

    $message preg_replace($p$r$message);
    print 
    $message
    Results in:
    Code:
    This is my message.  It has a <iframe width=\"100%\" src=\"blog-poll.php?pollid=171\" scrolling=\"no\" id=\"the_iframe\" onLoad=\"calcHeight();\" height=\"1px\" frameborder=\"0\" ></iframe> in it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New Coder
    Join Date
    Feb 2009
    Posts
    90
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hi Fou-Lu,

    Thanks that works great the regex finds the brackets and enclosed word poll and number. The only issue after checking the page source, the blog-poll.php?pollid=$1 is empty for the poll ID. How does $1 variable get its value from $p?

    The above any way is a great help, nice assignment of regex to the variable

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    $1 represents the value of the brackets located here: /\[poll(\d+)\]/i. $0 would be the entire matched string, which is typically a useless chunk for something like a preg_replace.
    I ran that test above on codepad (I was surprised it worked since they have issues with pattern compiling). The results are a copy/paste of the source it generated. It may be wiser to use backreference with \1 instead of $1.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    New Coder
    Join Date
    Feb 2009
    Posts
    90
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks to all for the help have finalised the php as

    PHP Code:
    // Insert Polls
     
    preg_match("/\[poll(.*)\]/"$message $poll_link);
    $poll_links "<iframe width=\"100%\" src=\"blog-poll.php?pollid=$poll_link[1]\" scrolling=\"no\" id=\"the_iframe\" onLoad=\"calcHeight();\" height=\"1px\" frameborder=\"0\" ></iframe>";
    $pollfind "/\[poll(\d+)\]/i";
    $message preg_replace($pollfind$poll_links$message);

    return(
    $message); 


    I need to try see if will accept multiple poles on the same page such as having more than one bracket [poll1] [poll2] ..etc


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •