Click to See Complete Forum and Search --> : PHP question - regular expressions, string replacement.
Nevermore
04-26-2003, 11:06 AM
Does anyone know how I could replace a string that starts with a certain character, and ends with a certain character, but has any number of, and combination of, other characters in between. I think it can be done with regular expressions, but haven't used them very much before.
BTW, in case you didn't read the title, I'm using PHP.
Thankyou.
khalidali63
04-26-2003, 11:16 AM
you can use substring string manipulation functions as well,for RegExp..hunmm..PM pyro..:D
lol... :D
Try this:
<script language="javascript" type="text/javascript">
var re = /^A.*Z$/; //set up reg exp to start with an uppercase A and end with an uppercase Z and take anything and everything in between
var mystring = "Ad3f24fj-@%(}lZ"; //string to replace
var mynewstring = mystring.replace(re, "yourreplacement") //replace mystring with "yourreplacement"
alert (mynewstring);
</script>
Nevermore
04-26-2003, 01:16 PM
Does anyone know if there is a PHP way to do this, as I am using PHP for the rest of the operation?
Oh boy... I didn't read you question very well... :( Here is the PHP version. (untested)
$re = /^A.*Z$/; //set up reg exp to start with an uppercase A and end with an uppercase Z and take anything and everything in between
$mystring = "Ad3f24fj-@%(}lZ"; //string to replace
$mynewstring = str_replace($re, "yourreplacement", $mystring) //replace mystring with "yourreplacement"
echo $mynewstring;
NOTE: if that doesn't work, let me know, and I'll get you a tested version... right now I'm in a hurry.
Nevermore
04-26-2003, 01:39 PM
Don't you need to use ereg_replace to use regular expressions? I can't get your code to work - any ideas why, or other things I could try?
Nevermore
04-26-2003, 02:04 PM
I have the replace feature working, but I have a new problem. How would I make it allow any character in between the two letters, rather than just the alphabet and numbers? I need to be able to allow basically every character.
Nevermore
04-26-2003, 02:17 PM
Never mind, I got it! Thanks for all of your help.
:D :D :D