0

I have a string in php that looks like this

$(window).load(function(){
  $('.someclass').click(function () {
    $(this).text("clicked");
  });
});

what i want is - if string contains $(window).load(function(){ then replace this and also the end braces }); with empty string ""

But if $(window).load(function(){ do not exist then do nothing.

Here is what i have tried:

if(strpos($str,"$(window).load(function(){") == -1){
  // do nothing
}
else{
     str_replace("$(window).load(function(){","",$str);
     // how do i replace the last }); with ""
}
4
  • Why do want to do this (what's the context)? You can't simply remove .load directly on the page? Commented Mar 28, 2013 at 14:19
  • I am not on page... i am on server with a php string.. Commented Mar 28, 2013 at 14:22
  • What if .load is written like this: $(window).load( function() { (note extra spaces). Arguably this can be resolved by using regex but IMO it will be an error prone operation. Commented Mar 28, 2013 at 14:24
  • i will always get that string without that extra space.. so you can be sure that their will be no extra space Commented Mar 28, 2013 at 14:36

5 Answers 5

1

If your code is nicely indented like that, this might just work for you:

$str = <<<EOM
$(window).load(function(){
  $('.someclass').click(function () {
    $(this).text("clicked");
  });
});
EOM;

$start = preg_quote('$(window).load(function(){');
$end = preg_quote('});');

$new = preg_replace("/^$start\s*(.*?)^$end/ms", '$1', $str);

print_r($new);
Sign up to request clarification or add additional context in comments.

3 Comments

i am getting string like this $str = file_get_contents('file.js'); .... can you please update your answer accordingly..
can i do this $str = <<<EOM file_get_contents('file.js'); EOM;
@JAVAGeek Then just replace that block with $str = file_get_contents('file.js');.
1

You will need regular expressions for this one if you can guarantee that the }); will be the last one. If so:

$str = preg_replace("#\$\(window\)\.load\(function\(\) \{(.*)\}\);#is","",trim($str));

Should do the trick.

If you cannot guarantee that the }); you want to replace will be the last occurence, you will have to walk through your code and count the braces. No other way, sadly :-(

7 Comments

You can replace that last $str with trim($str) just to ensure there's no extra returns or spaces.
if their is $(window).load(function(){ present then their will be end braces too always
what i did $str = file_get_contents('file.js'); $str = preg_replace("#\$\(window\)\.load\(function\(\) \{(.*)\}\);#i","",trim($str)); echo $str;
That confirms the issue I didn't state about this answer. Using preg_replace will check only the first line of text. It will not treat it as a glob. It stops at the \n. I believe the /s modifier will allow it to use the whole string as a string and not only the first line.
where do you put that /s
|
0
$str = substr($str, 0, strlen($str) - 4);

This will remove the last 3 characters of the string.

Comments

0

Find the position of the last occurrence with strrpos ? Then maybe do a str_replace from that point with a limit of 1? You should check the modified string with an external call to something like JSlint to make sure you didnt create malformed code.

Comments

0

I think, a working way will be just to test for (window).load, and to add this :

str_replace('$(window).load', "var functionOnLoad = ", $str);

Don't forget to add a call to this function if you want it to be execute. Somethink like :

str_replace('</script>', "functionOnLoad();</script>", $str);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.