/    Sign up×
Community /Pin to ProfileBookmark

Using preg_replace with i modifier instead pf str_ireplace using arrays.

Hi all,

I’m trying to replace the following:

[code=php]$tr=array(“<“,”>”,”n”,”[b]”,”[/b]”,”[i]”,”[/i]”,”[u]”,”[/u]”,”javascript”,”[red]”,”[/red]”,”[blue]”,”[/blue]”,”[yellow]”,”[/yellow]”,”[green]”,”[/green]”,”[black]”,”[/black]”,”[white]”,”[/white]”,”[sub]”,”[/sub]”,”[sup]”,”[/sup]”,”[move]”,”[/move]”,”[s]”,”[/s]”,”\””,”‘”,”****”,”bloody”,”****”,”****”);
$tr2=array(“&lt;”,”&gt;”,”<br />”,”<b>”,”</b>”,”<i>”,”</i>”,”<u>”,”</u>”,”java script”,”<font style=”color:red”>”,”</font>”,”<font style=”color:blue”>”,”</font>”,”<font style=”color:yellow”>”,”</font>”,”<font style=”color:green”>”,”</font>”,”<font style=”color:black”>”,”</font>”,”<font style=”color:white”>”,”</font>”,”<sub>”,”</sub>”,”<sup>”,”</sup>”,”<marquee>”,”</marquee>”,”<s>”,”</s>”,”&quot;”,”‘”,”****”,”******”,”****”,”****”);
$_POST[‘message’]=str_ireplace($tr,$tr2,$_POST[‘message’]);[/code]

With a preg_replace function.

Line 32 to 35 are what I have replaced the str_ireplace function with:

[code=php]for($trar=0;$trar<count($tr);$trar++){
$tr[$trar]=”/”.$tr[$trar].”/i”;
}
$_POST[‘message’]=preg_replace($tr,$tr2,$_POST[‘message’]);[/code]

But it doesn’t replace it.

File is attached.

Thanks,

Neil

[upl-file uuid=6968e6f3-01dd-4c72-b764-20c198544c3b size=5kB]shout2.txt[/upl-file]

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@scragarDec 20.2004 — [code=php]for($trar=0;$trar<count($tr);$trar++){
$_POST['message']=preg_replace(/$tr[$trar]/i,$tr2[$trar],$_POST['me
ssage']);
};[/code]
Copy linkTweet thisAlerts:
@neil9999authorDec 20.2004 — Thanks, but that doesn't seem to work (or atleast I can't get it to work!) Attached is my new code. I get the following error now:

Parse error: syntax error, unexpected '/', expecting ')' in C:Program FilesAbyss Web Serverhtdocsshoutshout3.php on line 33

Neil

[upl-file uuid=6b48a392-3ba7-4f3a-ac7d-b3282f1a154f size=5kB]shout3.txt[/upl-file]
Copy linkTweet thisAlerts:
@scragarDec 20.2004 — message should all go on the same line:

[code=php]for($trar=0;$trar<count($tr);$trar++){
$temp = $tr[$trar];
$_POST['message']=preg_replace(/$temp/i,$tr2[$trar],$_POST['message']);
};
[/code]


I forgot to correct that when I posted it, sorry.
Copy linkTweet thisAlerts:
@neil9999authorDec 20.2004 — Still get the same error, but on line 34.

Thanks again,

Neil

[upl-file uuid=d1f75535-003b-44cc-a23f-f3636e809fef size=5kB]shout3.txt[/upl-file]
Copy linkTweet thisAlerts:
@scragarDec 20.2004 — I don't know, just try using preg_ireplace and stuff making it a regular expression.
Copy linkTweet thisAlerts:
@neil9999authorDec 20.2004 — What do you mean? I don't think preg_ireplace is a function.

Or would you know how to do it with eregi_replace instead?

Thanks,

Neil
Copy linkTweet thisAlerts:
@scragarDec 20.2004 — ergi_iregplace is to be used for variuos thiings where you wish to keep information even though you replace it:

$msg = "[colour=red]this is some red text.[/colour]";

$msg = ereg_ireplace("[colour=([.*])]([.*])[/colour]","<font color=$1>$2</font>",$msg);

echo($msg); // writes "<font color=red>this is some red text</font>"

// onto the page.
Copy linkTweet thisAlerts:
@neil9999authorDec 20.2004 — Heyup, what I have now (attached) seems to replace rude words, but not tags like [ b ] (without spaces). Are the [ and ] s the source of the problem?

Thanks,

Neil

[upl-file uuid=f37a0fc0-869c-42d2-aaa2-7f2540fe57f4 size=5kB]shout3.txt[/upl-file]
Copy linkTweet thisAlerts:
@neil9999authorDec 20.2004 — It isn't working when I post any message now; I get all these errors:

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'b' in C:Program FilesAbyss Web Serverhtdocsshoutshout3.php on line 35

Warning: preg_replace() [function.preg-replace]: Unknown modifier ']' in C:Program FilesAbyss Web Serverhtdocsshoutshout3.php on line 35

Warning: preg_replace() [function.preg-replace]: Unknown modifier ']' in C:Program FilesAbyss Web Serverhtdocsshoutshout3.php on line 35

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'r' in C:Program FilesAbyss Web Serverhtdocsshoutshout3.php on line 35

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'b' in C:Program FilesAbyss Web Serverhtdocsshoutshout3.php on line 35

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'y' in C:Program FilesAbyss Web Serverhtdocsshoutshout3.php on line 35

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'g' in C:Program FilesAbyss Web Serverhtdocsshoutshout3.php on line 35

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'b' in C:Program FilesAbyss Web Serverhtdocsshoutshout3.php on line 35

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'w' in C:Program FilesAbyss Web Serverhtdocsshoutshout3.php on line 35

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'b' in C:Program FilesAbyss Web Serverhtdocsshoutshout3.php on line 35

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'p' in C:Program FilesAbyss Web Serverhtdocsshoutshout3.php on line 35

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'o' in C:Program FilesAbyss Web Serverhtdocsshoutshout3.php on line 35

Warning: preg_replace() [function.preg-replace]: Unknown modifier ']' in C:Program FilesAbyss Web Serverhtdocsshoutshout3.php on line 35

File attatched.

Thanks,

Neil

[upl-file uuid=e2d7398d-b551-40dd-b8b9-8ab54bf17407 size=5kB]shout3.txt[/upl-file]
Copy linkTweet thisAlerts:
@scragarDec 20.2004 — backslash any forward slashes:

[code=php]preg_replace(/[/b]/i,"</b>","irelevant for now...");
// should become:
preg_replace(/[/b]/i,"</b>","irelevant for now...");[/code]
Copy linkTweet thisAlerts:
@shimonDec 20.2004 — [i]Originally posted by scragar [/i]

[B]ergi_iregplace is to be used for variuos thiings where you wish to keep information even though you replace it:[/B][/QUOTE]


Umm no - not really. ereg and preg are simply different standards for regexes (POSIX extended and Perl-compatible respectively). The consensus seems to be that the preg_* family are faster and more powerful than ereg_*, though I have no data to hand to back that up.

Now then, this is irrelevant anyway, since in your case, you're not even using regular expressions. str_ireplace() seems like the right function to use - any reason why you decided against it?
Copy linkTweet thisAlerts:
@neil9999authorDec 20.2004 — [i]Originally posted by scragar [/i]

[B]backslash any forward slashes:



[code=php]preg_replace(/[/b]/i,"</b>","irelevant for now...");
// should become:
preg_replace(/[/b]/i,"</b>","irelevant for now...");[/code]
[/B][/QUOTE]


Ta, but that seems to crash the server.

Neil

[upl-file uuid=4837aa74-67c0-4df6-857f-5073ca2bb228 size=5kB]shout4.txt[/upl-file]
Copy linkTweet thisAlerts:
@neil9999authorDec 20.2004 — Can't use str_ireplace as it is PHP 5 only.

Thanks,

Neil
Copy linkTweet thisAlerts:
@shimonDec 20.2004 — [i]Originally posted by neil9999 [/i]

[B]Can't use str_ireplace as it is PHP 5 only.[/B][/QUOTE]


Fair enough - I guess I'm taking it for granted already ?

Well, if you choose to use a regex function, you'll need to make sure you escape all of the characters in your 'replace these' array which have special meaning to regexes. So /, [, and ] would all have to be escaped with a backslash.
Copy linkTweet thisAlerts:
@NogDogDec 20.2004 — Firstly, each preg search expression needs to be bounded by slashes, and can optionally have 1 or more qualifiers after it, such as:
[code=php]"/look for this/i"[/code]
Secondly, in the preg search expressions, since '/' is a delimiter and '[' starts a character class definition, they need to be escaped by backslashes:
[code=php]"/\[\/color=red]/"[/code]
Copy linkTweet thisAlerts:
@neil9999authorDec 20.2004 — Thanks, I escaped all / and [ with backslashes and it worked!

Attached is my code now.

Neil

[upl-file uuid=dad72fb4-8ab6-4509-a126-1459f12fd594 size=5kB]shout4.txt[/upl-file]
×

Success!

Help @neil9999 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...