Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 11 of 11

Thread: PHP: need help with preg_replace

  1. #1
    silverType's Avatar
    177
    posts
    Still getting nowhere

    PHP: need help with preg_replace

    Hi,
    I want to remove the onclick event from my html tag. For instance, given the following:

    <input type="text" name="txtEmail" id="txtEmail" onclick="parent.showTestStrategyDiv(this.id, this.name);" />

    'onclick' will be removed and the new string will look like this:

    <input type="text" name="txtEmail" id="txtEmail" />

    To do this, I call the preg_replace method in php:

    $alteredContent3 = preg_replace("#(<input.+?)(on[Cc]lick.+?)(/?>)#i", "\\1\\3", $alterContent2);

    Given that alterContent2 is the the html page which I want to alter. Somehow, when I tried to print out the values for $alteredContent3, nothing is returned. Any help is greatly appreciated. Thanks in advance.

  2. #2
    how are you getting "$alterContent2" i tried

    PHP Code:
    <?php
    $x 
    '<input type="text" name="txtEmail" id="txtEmail" onclick="parent.showTestStrategyDiv(this.id, this.name);" />';

    echo 
    preg_replace("#(<input.+?)(on[Cc]lick.+?)(/?>)#i""\\1\\3"$x );
    ?>
    and i get

    HTML Code:
    <input type="text" name="txtEmail" id="txtEmail" />
    so its all about how ur html is in $alterContent2

  3. #3
    silverType's Avatar
    177
    posts
    Still getting nowhere
    Quote Originally Posted by binime View Post
    how are you getting "$alterContent2" i tried

    PHP Code:
    <?php
    $x 
    '<input type="text" name="txtEmail" id="txtEmail" onclick="parent.showTestStrategyDiv(this.id, this.name);" />';

    echo 
    preg_replace("#(<input.+?)(on[Cc]lick.+?)(/?>)#i""\\1\\3"$x );
    ?>
    and i get

    HTML Code:
    <input type="text" name="txtEmail" id="txtEmail" />
    so its all about how ur html is in $alterContent2
    I see. Will go through my codes again verify that. Thanks for the tips.

  4. #4
    silverType's Avatar
    177
    posts
    Still getting nowhere
    Quote Originally Posted by silverType View Post
    I see. Will go through my codes again verify that. Thanks for the tips.
    Hi,

    I have another question. I am trying to modify the previous preg_replace statement to only affect button input type. The following is my code:

    $alteredContent3 = preg_replace("#(<input.*?(?:type=\"button\").*?)(o nclick.+?)(\/?>)#i", "\\1$strString\\3", $alteredContent);

    Once again, it didn't work. I was thinking that perhaps the quotes are not escape properly. Any suggestions? Thanks again.

  5. #5
    silverType's Avatar
    177
    posts
    Still getting nowhere
    Quote Originally Posted by silverType View Post
    Hi,

    I have another question. I am trying to modify the previous preg_replace statement to only affect button input type. The following is my code:

    $alteredContent3 = preg_replace("#(<input.*?(?:type=\"button\").*?)(o nclick.+?)(\/?>)#i", "\\1$strString\\3", $alteredContent);

    Once again, it didn't work. I was thinking that perhaps the quotes are not escape properly. Any suggestions? Thanks again.

    Ok, think i figured it out. Quotes can be escaped with 3 backslashes...as such, the preg_replace statement would be:

    $alteredContent3 = preg_replace("#(<input.*?(?:type=\\\"button\\\").* ?)(onclick.+?)(\/?>)#i", "\\1$strString\\3", $alteredContent);

    Cheers.

  6. #6
    silverType's Avatar
    177
    posts
    Still getting nowhere

    Yet another question

    I have yet another question. Bascally, now I need to substitute the parameters in the preg_replace with arrays. Below's my code:

    $patterns[0] = "#(<(?:input|textarea).+?)(/?>)#";
    $patterns[1] = "#(<img(?:.+?)src=(?:'|\")?)([^\"].+?)(/?>)#";
    $patterns[2] = "#(<input.*?(?:type=\\\"button\\\").*?)(onclick.+? )(\/?>)#i"

    $replace[0] = "\\1 onclick=\"parent.showTestStrategyDiv(this.id, this.name)\" \\2";
    $replace[1] = "\\1$host\\2\\3";
    $replace[2] = "\\1\\3";

    $alteredContent3 = preg_replace($patterns, $replace, $content);

    As before, these statements didn't work and an 'Parse error: syntax error, unexpected T_VARIABLE ' error was thrown. Any help would be greatly appreciated. Thanks again.

  7. #7
    Well...
    Code:
    $patterns[0] = "#(<(?:input|textarea).+?)(/?>)#";
    $patterns[1] = "#(<img(?:.+?)src=(?:'|\")?)([^\"].+?)(/?>)#";
    $patterns[2] = "#(<input.*?(?:type=\\\"button\\\").*?)(onclick.+? )(\/?>)#i"
    is no longer regex, it's being translated literally, so what you see is what you get. hopefully that points you in right direction.
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  8. #8
    silverType's Avatar
    177
    posts
    Still getting nowhere
    Quote Originally Posted by simplistik View Post
    Well...
    Code:
    $patterns[0] = "#(<(?:input|textarea).+?)(/?>)#";
    $patterns[1] = "#(<img(?:.+?)src=(?:'|\")?)([^\"].+?)(/?>)#";
    $patterns[2] = "#(<input.*?(?:type=\\\"button\\\").*?)(onclick.+? )(\/?>)#i"
    is no longer regex, it's being translated literally, so what you see is what you get. hopefully that points you in right direction.
    Hi simplistik,

    Thanks for the reply. Anyway, I need it to work like regex. Any suggestions on how I can come about doing it?

  9. #9
    Quote Originally Posted by silverType View Post
    Hi simplistik,

    Thanks for the reply. Anyway, I need it to work like regex. Any suggestions on how I can come about doing it?
    You know what I was wrong, I just relooked at your code and all you have to do is add a ; after
    PHP Code:
    $patterns[2] = "#(<input.*?(?:type=\\\"button\\\").*?)(onclick.+? )(\/?>)#i" 
    to
    PHP Code:
    $patterns[2] = "#(<input.*?(?:type=\\\"button\\\").*?)(onclick.+? )(\/?>)#i"
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  10. #10
    silverType's Avatar
    177
    posts
    Still getting nowhere
    I see. Can't believe that I actually missed that. LOL...Anyway, thanks a lot.

  11. #11
    1,627
    posts
    hugeExplosions = true;
    unexpected T_SOMETHING errors are 99.9% of the time syntax errors in code!
    MS Paint FTW!


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012