0

I am stuck.

I am trying to see if an textarea contains an #.

This is what I am trying but it doesnt work.

Example:

#firstname# - should trigger "processTaggedMessage"

whereas

firstname - should trigger "processNormalMessage"

The code (PHP):

(preg_match("^#(.*?)#$", $msg))
  ? $this->processTaggedMessage($fk_uid, $fk_cid, $sender, $msg, $num, $q)
  : $this->processNormalMessage($fk_uid, $fk_cid, $sender, $msg, $num, $q);

I am sure it is probs something simple but I cannot see it, I believe the regex is correct :S

Thanks,

Kyle

4 Answers 4

1

Get rid of the ^ and $ if you're trying to match several substrings delimited by #.

Use:

preg_match('/#.*?#/s', $msg)
Sign up to request clarification or add additional context in comments.

2 Comments

Note that using a greedy match (.*?) is less efficient that specifying what you don't want to match ([^#]*).
@Daniel: Correction - .* is greedy. .*? is lazy.
0

try

preg_match('/^#(.*?)#$/', $msg)

2 Comments

Example: Hello #frstname#, your appointment is with #appt# at #time# on #date#
then, your problem is '^' and '$', try without them. for example, '/^firstname$/' means that firstname match all your string from begining (because ^), to end ($).
0

try

if (preg_match('/^(.*)#(.*)$/', $msg))

and you could also take a look here:

http://php.net/preg_match

and you will find this:

Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.

4 Comments

The OP is looking for a non-specific string (ie. text situated between two #'s). There is no way to match this efficiently with fixed-string methods like strpos or strstr.
the problem was described as "I am trying to see if an textarea contains an #."
for that you could use if (preg_match('/^(.*)#(.*)#(.*)$/', $msg))
Maybe I should have been more descriptive. @Stillstanding came up with the correct solution. Ty though
0

PCRE regular expressions in PHP need to be delimited. You can try:

preg_match('/^#([^#]*)#$/', $msg)

Note that ^ and $ (as per your original pattern) specify that the pattern is anchored to the beginning and end of the string. This means your pattern will not match unless the entire string is #firstname#. If you want to search for #'s within a string, remove the anchors:

preg_match('/#([^#]*)#/', $msg)

Also, if you want to ensure that there is some text between the hashes, you should use + instead of * (/#([^#]+)#/). * means "zero or more repetitions", whereas + means "one or more repetitions".

By the way, since you mentioned in the comments that there are multiple occurrences of what you're searching for within your target string, you'll likely want to use preg_match_all instead of preg_match so that you can collect all the matches.

8 Comments

@Daniel V: This didn't seem to match anything. Ty though :D
@Kyle I'm not sure why it wouldn't match, it's a pretty straightforward pattern and works fine for me... what is the output if you var_dump($msg)?
@Daniel V: Hello #frstname#, your appointment is with #appt# at #time# on #date#.
@Kyle in that case you don't want your pattern to be anchored. See my update.
@Daniel V: Thanks for the update, it still doesn't match anything? I must be missing something?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.