Porl123 Posted September 24, 2009 Share Posted September 24, 2009 I was kind of hoping that someone could help me with this regex pattern I've been trying to write and failing it's for directing a comment at someone on the forums and would by typed out as: /username messageblablabla and would come out: <span style="color:cyan">TO: <b>username</b>: messageblablabla</span> I managed it by exploding the string and piecing it all together but it had the problem that I couldn't determine where the line break was, so it wouldn't carry on after it goes on to a new sentence. I thought my first way was a little impractical too so I was wondering if I could do this using preg_match If anyone can help me you're doing me a real favour because it's been racking my brain for hours. thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/175360-regex-pattern/ Share on other sites More sharing options...
Adam Posted September 24, 2009 Share Posted September 24, 2009 Provided there's no white space allowed within the username, you could use: if (preg_match('#^/([^\s]+)\s(.+?)$#', $input_str, $matches)) { print_r($matches); } Quote Link to comment https://forums.phpfreaks.com/topic/175360-regex-pattern/#findComment-924104 Share on other sites More sharing options...
Porl123 Posted September 24, 2009 Author Share Posted September 24, 2009 Array ( [0] => /name message\r\n\r\nfollowingmessage [1] => name [2] => message\r\n\r\nfollowingmessage ) That's pretty much doing it but it's reading the followingmessage two lines down as the message to the name. any clue how I'd get that to not include after the line break? Quote Link to comment https://forums.phpfreaks.com/topic/175360-regex-pattern/#findComment-924109 Share on other sites More sharing options...
Adam Posted September 24, 2009 Share Posted September 24, 2009 You mean to only return the first line? Just modify the reg exp slightly: #^/([^\s]+)\s([^\s]+)# Quote Link to comment https://forums.phpfreaks.com/topic/175360-regex-pattern/#findComment-924119 Share on other sites More sharing options...
Porl123 Posted September 24, 2009 Author Share Posted September 24, 2009 Array ( [0] => /name message [1] => name [2] => message ) So close the message was "message test test" and it's cut off the test test Quote Link to comment https://forums.phpfreaks.com/topic/175360-regex-pattern/#findComment-924125 Share on other sites More sharing options...
Adam Posted September 24, 2009 Share Posted September 24, 2009 Edit: Try this: if (preg_match('#^/([^\s]+)\s(.+?)#', $input_str, $matches)) { print_r($matches); } Quote Link to comment https://forums.phpfreaks.com/topic/175360-regex-pattern/#findComment-924128 Share on other sites More sharing options...
Porl123 Posted September 24, 2009 Author Share Posted September 24, 2009 Array ( [0] => /name m [1] => name [2] => m ) Not too sure about that one ^^ Quote Link to comment https://forums.phpfreaks.com/topic/175360-regex-pattern/#findComment-924133 Share on other sites More sharing options...
nrg_alpha Posted September 24, 2009 Share Posted September 24, 2009 If the string only contains the username and message and contains carriage returns and newlines perhaps something along the lines of: $str = <<<EOF /Username This is only a test! So there! That's that! EOF; preg_match('#^/([^\s]+)[\R\s]*(.*)#s', $str, $match); array_shift($match); echo "<span style=\"color:cyan\">TO: <b>$match[0]</b>: $match[1]</span>"; ? If not, perhaps provide a sample string (formated exactly as is). EDIT - Sorry for the initial bad post formatting.. still having issues with Chrome and this site's posting system botching stuff. Posting in Opera for now. Quote Link to comment https://forums.phpfreaks.com/topic/175360-regex-pattern/#findComment-924145 Share on other sites More sharing options...
Porl123 Posted September 24, 2009 Author Share Posted September 24, 2009 Array ( [0] => /name message test test\r\n\r\nfollowing message [1] => name [2] => message test test\r\n\r\nfollowing message ) Almost had it again it's got the name but the following message is linked to the "message test test" Quote Link to comment https://forums.phpfreaks.com/topic/175360-regex-pattern/#findComment-924147 Share on other sites More sharing options...
dgoosens Posted September 24, 2009 Share Posted September 24, 2009 if there are no whitespaces allowed in the username... just do a strpos and substr $str = "/username messageblablabla"; $userName = substr($str, 1, strpos($str, " ")); $message = substr($str, strpos($str, " ")); hope that works Quote Link to comment https://forums.phpfreaks.com/topic/175360-regex-pattern/#findComment-924148 Share on other sites More sharing options...
Porl123 Posted September 24, 2009 Author Share Posted September 24, 2009 Yeah I thought of a few ways around preg_match but thinking about it preg_match can handle matching more than one at a time and identifies the line breaks Quote Link to comment https://forums.phpfreaks.com/topic/175360-regex-pattern/#findComment-924152 Share on other sites More sharing options...
thebadbad Posted September 24, 2009 Share Posted September 24, 2009 You have to be clear. Line breaks aren't allowed in the message, right? Then try this: <?php $str = '/username messsage with spaces! test test. Inline message: /seconduser new message.'; echo preg_replace('~/(\S+) ([^\r\n]+)~', '<span style="color: cyan;">TO: <strong>$1</strong>: $2</span>', $str); ?> It will stop matching the message as soon as an \r or \n are encountered. Quote Link to comment https://forums.phpfreaks.com/topic/175360-regex-pattern/#findComment-924159 Share on other sites More sharing options...
nrg_alpha Posted September 24, 2009 Share Posted September 24, 2009 On the issue of clearness, Porl123, I advise you to have a look at this thread. If my previous response doesn't solve the issue at hand, provide some example strings (more than one if there is a variance in formatting and whatnot) instead of simply outputting the end result array. Describe in detail what is (and what is not) permissible. The more info you provide (within reason, this is not suggest dumping a mountain of information), the better. Less guessing going on back and forth. Better initial communication makes problem resolutions that much smoother and faster. Quote Link to comment https://forums.phpfreaks.com/topic/175360-regex-pattern/#findComment-924162 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.