My original string looks like this
value='' tabindex='500'
I would like to replace the string with this
value='Email:' tabindex='500' onclick=\"if (this.value=='Email:') {this.value='';}
So I use this str_replace function
$userForm = str_replace("value='' tabindex='500'", "value='Email:' tabindex='500' onclick=\"if (this.value=='Email:') {this.value='';}\"",$userForm);
My output ends up being this
value="" tabindex="500" onfocus=" if (this.value == '') { this.value = ''; }"
If you notice, everything except for the parts that include 'value="Email:" are replaced correctly...
I was hoping someone might have an idea on why this is happening? Any help would be greatly appreciated. Thank you in advance!
You may be looking for something like:
$userForm = preg_replace('/value=\'\' tabindex=\'(.*?)\'/i', 'value=\'Email:\' tabindex=\'$1\' onclick="if (this.value==\'Email:\') {this.value=\'\';}', $userForm);
Result:
value='Email:' tabindex='500' onclick="if (this.value=='Email:') {this.value='';}
You can use below codes:
$val = "value='' tabindex='500'";
$result = str_replace( "value='' tabindex='500'", "value='Email:' tabindex='500' onclick=" . '\"if' . " (this.value=='Email:') {this.value='';}", $val );
echo $result;
Something is missing here, copying your code right off:
<?php
$userForm = "value='' tabindex='500'";
var_dump($userForm);
echo '<br><br>';
$userForm = str_replace("value='' tabindex='500'", "value='Email:' tabindex='500' onclick=\"if (this.value=='Email:') {this.value='';}\"",$userForm);
var_dump($userForm);
I end up with the following result:
value='Email:' tabindex='500' onclick="if (this.value=='Email:') {this.value='';}"
which seems to be the results you are looking for. Do you alter the variable anywhere else?