PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Nov 21, 2019 8:17 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: ^ & $ used differently?
PostPosted: Thu May 26, 2005 10:46 pm 
Offline
Forum Regular
User avatar

Joined: Sat Mar 12, 2005 8:13 pm
Posts: 703
Location: US


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 5:09 am 
Offline
DevNet Resident
User avatar

Joined: Sat Dec 06, 2003 10:52 pm
Posts: 1679
Location: Mumbai, India
Syntax: [ Download ] [ Hide ]
<?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 &amp; with a whitespace too.


Last edited by anjanesh on Fri May 27, 2005 5:55 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 5:18 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
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

Syntax: [ Download ] [ Hide ]
preg_replace('/\br\b/i', 'are', $data);


Also to avoid doing this ----&gt; \r

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

Syntax: [ Download ] [ Hide ]
preg_replace('/(?<!\\)\br\b/i', 'are', $data);


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 5:27 am 
Offline
DevNet Resident

Joined: Tue Nov 18, 2003 2:09 pm
Posts: 1826
Location: Middlesbrough, UK
something this simple i would generally not use preg

eg of what i do..
Syntax: [ Download ] [ Hide ]
$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


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 5:44 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 1:10 pm 
Offline
Forum Regular
User avatar

Joined: Sat Mar 12, 2005 8:13 pm
Posts: 703
Location: US
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.
Syntax: [ Download ] [ Hide ]
$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);


Top
 Profile  
 
 Post subject: Also
PostPosted: Sat May 28, 2005 2:21 am 
Offline
Forum Contributor

Joined: Fri May 03, 2002 10:33 am
Posts: 108
Location: Cheshire
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group