Xeoncross Posted January 2, 2008 Share Posted January 2, 2008 Well, we all know the php code for if/else conditional statements. <?php $var = ($value ? true : false); ?> However, I can't find any data on the regex version. I know I read somewhere you can assign values based on conditions - I just can't find it. This is wrong, but I am thinking of something like: (?([a-zA-Z]+)|[0-9]) Where you would could set the output "\\0" to "4' or "somestring". I could always use preg_replace_callback() to check for values - but I want to keep all my checking within the regex. So can anyone help me with this? Quote Link to comment Share on other sites More sharing options...
rea|and Posted January 3, 2008 Share Posted January 3, 2008 The syntax you are using is also a conditional statement; you can set two ways of matching a string in the same regexp. I.e. <?php # if a line starts w/ a number then match something, otherwise something else #(?(condition)true case|false case) $rex='/^(?(?=\d+)something|somethingelse)/m'; In a way you could set your output, but it is not its first aim. I'd keep on using a callback function to check your values. Quote Link to comment Share on other sites More sharing options...
Xeoncross Posted January 3, 2008 Author Share Posted January 3, 2008 Thanks I couldn't find anything at first because I didn't know what this stuff was called. (Lookahead Assertions). So I have two questions, first when you create an "Assertion" does it count as one of your "\\0" replacement groups or not? Second, I would like to check for a "http://, ftp://" start to a url - and if there isn't one add "http:" to the front of it. (if there is a "[a-z]+://" then don't add anything) Can you look at my code and see what I am doing wrong? <?php $text = ' http://site.com '. "\n". ' site.com '; $text = preg_replace("/(?(?=[a-zA-Z]+:\/\/)thevalueintheassertion|http:\/\/)/", "\\0", $text); ?> Here are some pages I found on the subject: http://jregex.sourceforge.net/syntax.html#asserts http://www.regular-expressions.info/lookaround.html http://docs.python.org/lib/re-syntax.html Quote Link to comment Share on other sites More sharing options...
effigy Posted January 3, 2008 Share Posted January 3, 2008 when you create an "Assertion" does it count as one of your "\\0" replacement groups or not? No. Lookarounds look, they do not capture. The bottom part of this topic covers checking for "http://" at the beginning. Quote Link to comment Share on other sites More sharing options...
rea|and Posted January 3, 2008 Share Posted January 3, 2008 [...] Second, I would like to check for a "http://, ftp://" start to a url - and if there isn't one add "http:" to the front of it. (if there is a "[a-z]+://" then don't add anything) Can you look at my code and see what I am doing wrong? Well, you could not hard-set your output within your regular expression. What I've told before is that using a conditional statement you will know in advance that your output will be "A" or "B" match, but you couldn't force your output to be "A", "B" or some thing else. You needs to work with a callback function to check & force your output. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.