^ & $ used differently?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

^ & $ used differently?

Post by Skara »

I'm trying to do the thing here where it fixes where people are too lazy to type are and type "r" instead.

Erhm.. Basically, would something like this work?

Code: Select all

#(^|\s)blah(\s|$)#i
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Code: Select all

<?php
$text = "You r a whatever";
$pattern = "/(^|\s)r(\s|$)/is";
$replacement = " are ";
echo preg_replace($pattern,$replacement,$text);
?>
But this is replacing \s,^ and & with a whitespace too.
Last edited by anjanesh on Fri May 27, 2005 5:55 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Could you explain yourself a bit better please?

You mean you wanna replace "r" with "are"?

Use the \b metacharacter to make sure it's a single r

Code: Select all

preg_replace('/\br\b/i', 'are', $data);
Also to avoid doing this ----> \r

Look to see it doesn't follow a backslash as follows

Code: Select all

preg_replace('/(?<!\\)\br\b/i', 'are', $data);
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

something this simple i would generally not use preg

eg of what i do..

Code: Select all

$content = "u r a cool person";
// create arrays
$array_original = array("u", "r"); // words to replace
$array_replace = array("you", "are"); // words to replace WITH
// now convert
$content = str_replace($array_original, $array_replace, $content);
usage: just keep adding to the arrays, make sure that they are in the appropriate part of the array
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

malcolmboston wrote:something this simple i would generally not use preg

eg of what i do..

Code: Select all

$content = "<span style='color:blue' title='ignorance is bliss'>you</span> <span style='color:blue' title='ignorance is bliss'>are</span> a cool person";
// create arrays
$array_original = array("<span style='color:blue' title='ignorance is bliss'>you</span>", "<span style='color:blue' title='ignorance is bliss'>are</span>"); // words to replace
$array_replace = array("you", "are"); // words to replace WITH
// now convert
$content = str_replace($array_original, $array_replace, $content);
usage: just keep adding to the arrays, make sure that they are in the appropriate part of the array
There's a big advantage to preg_ with some words.

I won't type them in here since they'll be <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> 'd.

Also the escaping \ thing i showed is regex based.

But yeah, str_replace does a mdoest enough job ;)
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

str_replace isn't near enough for this.

d11wdq, thanks. You didn't escape your backslash enough, though. :P (\\\\)

If you're interested, I ended up with this. I'll probably have to edit and fix things later, but this'll do for now.

Code: Select all

$bad = array(
  '/(?<!\\\\)\br\b/i',
  '/(?<!\\\\)\bn\b/i',
  '/(?<!\/)\bu\b/i',
  '/\b(t)h(?:an)x\b/i',
  '/&#33;&#33;(?:&#33;)+/',
  '/\by\b/i',
  '/(?<!\/|\[)\bi\b/',
  '/\b(n)oone\b/i',
  '/\bdat\b/i',
  '/\bsi\b/i',
  '/\b(sooo)o+\b/i',
  '/\b(d)ud\b/i',
  '/\bur\b/i',
  '/\b(y)oure\b/i',
  '/\bi(m|ll|ve)\b/i',
  '/\b(ca|wo|do|did|was)nt\b/i',
  '/\b(p)pl\b/i',
  '/\b(w)elcom\b/i',
  '/\bnoe\b/i',
  '/\b(m)abye\b/i',
  '/\b(g)[2t](g)\b/i',
  '/\bne(?=\b|one)/i',
  '/\bne1\b/i',
  '/\b(r)ealli\b/i',
);
$b = '<span style="color: #00c;" class="ltt">'; $e = '</span>';
$good = array(
  $b.'are'.$e,
  $b.'in'.$e,
  $b.'you'.$e,
  $b.'\\1hanks'.$e,
  $b.'!!'.$e,
  $b.'why'.$e,
  $b.'I'.$e,
  $b.'\\1o one'.$e,
  $b.'that'.$e,
  $b.'is'.$e,
  $b.'\\1'.$e,
  $b.'\\1ude'.$e,
  $b.'you\'re'.$e,
  $b.'\\1ou\'re'.$e,
  $b.'I\'\\1'.$e,
  $b.'\\1n\'t'.$e,
  $b.'\\1pl'.$e,
  $b.'\\1elcome'.$e,
  $b.'know'.$e,
  $b.'\\1aybe'.$e,
  $b.'\\1ot to \\2o'.$e,
  $b.'any'.$e,
  $b.'anyone'.$e,
  $b.'\\1eally'.$e,
);
$txt = preg_replace($bad,$good,$txt);
leenoble_uk
Forum Contributor
Posts: 108
Joined: Fri May 03, 2002 10:33 am
Location: Cheshire
Contact:

Also

Post by leenoble_uk »

Most of the time you could probably get away with replacing loose with lose since that's in much more common use yet hardly anyone knows how to spell it.

There must be some way in regex to try and guess what the context of the word loose is to determine whether to swap it out or not.

It should also place an <a> tag with title attribute around the resultant word which explains why it has been re-spelt correctly.

Oh no I've gone and given myself a project which I simply have to complete before I can get any work done.
Post Reply