Jump to content

regex pattern


Porl123

Recommended Posts

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! :)

Link to comment
Share on other sites

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? :o

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.