Jump to content

end of word match


Destramic
Go to solution Solved by Psycho,

Recommended Posts

hey im tring to match words which contain double s at the end...ie. address, business, class etc...so that is so i can put a ' at the end...

class'

			if (preg_match("/ss$/", $name))
			{
				$name = $name . "'";
			}

any help with the regular expression would be great thank you

Link to comment
Share on other sites

  • Solution

That code works perfectly fine.

$name = "Address";
if (preg_match("/ss$/", $name))
{
    $name = $name . "'";
}
 
 echo $name; //Output Address'

I'm guessing that the values may not contain what you think they contain. Are there any spaces or other non-printable characters at the end? You may want to trim() them first.

 

But, regular expressions are slow and should be avoided if there are string functions you can use as an alternative. Also, there is no need to use preg_match() function to then add an additional character. You could just use the preg_replace() function. But, I would not do this with RegEx anyway. I would use string functions. Just use substr() to get the last two characters and see if they are both 's'.

$name = "Address";
 
$name = trim($name);
if(strtolower(substr($name, -2))=='ss')
{
    $name .= "'";
}
 
 echo $name; //Output Address'

If that does not work, then do a var_dump() on $name before the function and post the results here.

Edited by Psycho
Link to comment
Share on other sites

  • 4 weeks later...

Just a note, as this may be your problem. In regexp "$" matches EOL (end of line), that means if you have multiple words you want to match in a single line it will not work. You may instead be looking for "\b" which is a word boundary character. A word boundary is usually triggered by a space or punctuation.

 

I do agree with @Psycho on not using RegExp if it's not necessary. It's simply one of the many tools at our disposal and should be weighed against all other options before being selected as the right tool.

 

However were I to use regex I would process against an entire paragraph, or block of text like this:

preg_replace('/(\w+ss)\b/', '$1\'', $mytext);

 

I hope this was helpful to the op or anyone else who comes across this thread.

 

Edited by The Letter E
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.