Page 1 of 1

^ & $ used differently?

Posted: Thu May 26, 2005 10:46 pm
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

Posted: Fri May 27, 2005 5:09 am
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.

Posted: Fri May 27, 2005 5:18 am
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);

Posted: Fri May 27, 2005 5:27 am
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

Posted: Fri May 27, 2005 5:44 am
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 ;)

Posted: Fri May 27, 2005 1:10 pm
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);

Also

Posted: Sat May 28, 2005 2:21 am
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.