www.webdeveloper.com
Results 1 to 8 of 8

Thread: PHP question - regular expressions, string replacement.

  1. #1
    Join Date
    Feb 2003
    Location
    Britain
    Posts
    1,335

    PHP question - regular expressions, string replacement.

    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.

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    2,653
    you can use substring string manipulation functions as well,for RegExp..hunmm..PM pyro..

  3. #3
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    lol...

    Try this:

    Code:
    <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>

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  4. #4
    Join Date
    Feb 2003
    Location
    Britain
    Posts
    1,335
    Does anyone know if there is a PHP way to do this, as I am using PHP for the rest of the operation?

  5. #5
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Oh boy... I didn't read you question very well... Here is the PHP version. (untested)

    PHP Code:
    $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.

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  6. #6
    Join Date
    Feb 2003
    Location
    Britain
    Posts
    1,335
    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?

  7. #7
    Join Date
    Feb 2003
    Location
    Britain
    Posts
    1,335
    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.

  8. #8
    Join Date
    Feb 2003
    Location
    Britain
    Posts
    1,335
    Never mind, I got it! Thanks for all of your help.


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
  •  
HTML5 Development Center



Recent Articles