Loony

Started on the old forum in January 2009 · view the original in the archive · old links redirect here

15 replies · 690 views · started 17 years ago · 2 min read

5 participants
subzero Super moderator Fellow · 3,954 posts #1

To replace a part of the string with another string we will use str_replace function in PHP. This function will replace all the occurrence of the string we target and will replace with the string we supply. Here is an example.

<?
$string="This is an example on how html is used in 
developing user-friendly web sites. By using HTML  we can
 control the content of a page";

echo $string;
echo "

";
$string=str_replace("html","PHP",$string);

echo "[b]After replace[/b] 

$string";

?>
If you run the above code your will see result like this 

This is an example on how html is used in developing user-friendly web sites. By using HTML we can control the content of a page

After replace

This is an example on how PHP is used in developing user-friendly web sites. By using HTML we can control the content of a page

So you can see the text html is replace by PHP but HTML is not replaced. 
So str_replace() is a case sensitive string replacing function.

We can use str_ireplace() to get case insensitive string replacement

The main difference between str_replace and str_ireplace is str_replace is a case sensitive string replacement and str_ireplace is a case in-sensitive string search and replacement. 

str_ireplace function in PHP when applied to a main string, it searches for occurrence of a string and replace them by another string. 

This function can take an array as search string and each element of the array if present is replaced by another string. We can try with an example. If you are searching for a simple example then you can see str_replace function. Here is a sample code which takes an array of search words and replaces them by another word within a string. 

$search = array("good", "php", "html", "ASP");
$final_string = str_replace($search, "*", "All good sites uses PHP along with HTML or they use ASP and html");
echo $final_string;

As we have seen here we can use an array as search terms and all the elements of the array can be replaced by a replace string. We can also use another array as replacement array and each element is replace by matching element from replacement array, if not element is found then blank string is replaced. Here is the script code for that 

$search = array("good", "php", "html", "asp");
$replace=array("GOOD","PHP","HTML","ASP","NO");
$final_string = str_replace($search, $replace, "All good sites uses PHP along with HTML or they use asp and html");
echo $final_string;

Visit: Chat4u.mobi - The New Lay Of being a site of your dreams! Visit: WapMasterz Coming Back Soon!


SCRIPTS FOR SALE BY SUBZERO Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders. FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others... _______…

looony Developer · 95 posts #2

that helps me a little im tring to script my new chatroom and i wana add commands in chat like ban=loony will ban loony but i cant get it to ban how would i use the string in a mysql command

External image (may be gone)

if(isset($_SESSION['username'])) { print "everybody is a little loony inside"; } else { print"You should be shot"; }

kaibaspoon Developer · 63 posts #3

Dont know if this might help

CODE<?php $IP = $_SERVER['REMOTE_ADDR']; $IP_array = file('../folder/IP.dat'); $IP_array = str_replace("\n", "", $IP_array); $IP_array = str_replace("\r", "", $IP_array); foreach ($IP_array as $IP_test) { (substr_count($IP, $IP_test) != "0") {

echo 'Banned World!';

die(); } }

echo 'Hello World!';

?php>

subzero Super moderator Fellow · 3,954 posts #4

first of all mate

make a line of them test one out of one

Visit: Chat4u.mobi - The New Lay Of being a site of your dreams! Visit: WapMasterz Coming Back Soon!


SCRIPTS FOR SALE BY SUBZERO Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders. FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others... _______…

kaibaspoon Developer · 63 posts #5

This function is implemented in mysql and requires 3 fields, replace(coulumn name , search, replace) and more faster than php’s str_replace(). Bacause while using str_replace() you need to fetch each field from mysql database and replace searched string in php and then update the field in mysql database. if you use this function you only need to run single sql query.

SQLUPDATE table SET coulumn = REPLACE(coulmn,’search’,'replace’);

looony Developer · 95 posts #6

ive tryed about 20 difrent ways but cant seem to get it to work

External image (may be gone)

if(isset($_SESSION['username'])) { print "everybody is a little loony inside"; } else { print"You should be shot"; }

kaibaspoon Developer · 63 posts #7

Well then I'm just as lost as you in this case 🤔

Anshul Senior · 296 posts #8

QUOTE

that helps me a little im tring to script my new chatroom and i wana add commands in chat like ban=loony will ban loony but i cant get it to ban how would i use the string in a mysql command

ok here we go like ban/username

$checkmesage = explode("/",$msg);
if ($checkmesage[0]=="ban")
{
$againstuser=$checkmesage[1];
$queryaction = mysql_query("update `usertable` set ban='wateveurban' where login='$againstuser';");
}
looony Developer · 95 posts #9

that dont work either

$name = mysql_real_escape_string($_GET['name']);
                 $message = mysql_real_escape_string($_POST['message']);
    $room = mysql_real_escape_string($_POST['roomname']);
    $time = time();
    $name = mysql_real_escape_string($_GET['name']);
    addonline($username);
        $checkmessage = explode("/",$message);
if ($checkmesage[1]=="ban")
{
$againstuser=$checkmesage[1];
mysql_query("UPDATE 'users' set level_access='6' where username='$againstuser';");

}

External image (may be gone)

if(isset($_SESSION['username'])) { print "everybody is a little loony inside"; } else { print"You should be shot"; }

looony Developer · 95 posts #11

lol no need for sql but the snippet is above

External image (may be gone)

if(isset($_SESSION['username'])) { print "everybody is a little loony inside"; } else { print"You should be shot"; }

Anshul Senior · 296 posts #12

dude atleast copy things properly

<?php
$name = mysql_real_escape_string($_GET['name']);
$message = mysql_real_escape_string($_POST['message']);
$room = mysql_real_escape_string($_POST['roomname']);
$time = time();
addonline($username);
$checkmessage = explode("/",$message);
if ($checkmesage[0]=="ban")
{
$againstuser=$checkmesage[1];
$ban = mysql_query("UPDATE 'users' set level_access='6' where username='$againstuser';");
}
?>
looony Developer · 95 posts #13

i changed that see if it worked cause it didnt work the other way

External image (may be gone)

if(isset($_SESSION['username'])) { print "everybody is a little loony inside"; } else { print"You should be shot"; }

looony Developer · 95 posts #14

it dont work

External image (may be gone)

if(isset($_SESSION['username'])) { print "everybody is a little loony inside"; } else { print"You should be shot"; }

something else Fellow · 2,296 posts #15

i mean the code above is perfect so there must be something your doing wrong when adding it to your script or sql so without seeing them its kinda hopless trying to help 😛

Anshul Senior · 296 posts #16

wat message u post it wiLL be like this ban/username

Sign in to reply.

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close