Preg Replace Error

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 4480 Views - Last Post: 23 December 2011 - 01:28 PM Rate Topic: -----

#1 shebby  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 29-June 11

Preg Replace Error

Posted 20 December 2011 - 09:09 PM

line 129- function filterBadWords($str)
 {
    $result = mysql_query("SELECT badwords FROM clean") or die(mysql_error()); 
    $replacements = "*";
    
    while($row = mysql_fetch_assoc($result))
    {     
          $bad_words= $row['badwords'];
          $spaced_bad_words = preg_replace("/(.)/i", "\${1} ", $bad_words);
          $spaced_bad_words = trim($spaced_bad_words);
          $str = preg_replace("/$bad_words | $spaced_bad_words/i", str_repeat('*', strlen($bad_words)), $str);
    }
    
    return $str;
Line 143 - }


I go this error "Warning: preg_replace() [function.preg-replace]: Compilation failed: unmatched parentheses at offset 1 in C:\xampp\htdocs\login\chatlog3.php on line 139"

This post has been edited by Atli: 21 December 2011 - 04:14 AM
Reason for edit:: Don't post your entire text bold!


Is This A Good Question/Topic? 0
  • +

Replies To: Preg Replace Error

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3716
  • View blog
  • Posts: 5,976
  • Joined: 08-June 10

Re: Preg Replace Error

Posted 21 December 2011 - 04:31 AM

  • What data are you getting from the database? Clearly something in the data is not right, because the line triggering the error is complaining about a missing parentheses, but you have no parentheses in there; only the data from the database.

  • What is this supposed to accomplish?
    preg_replace("/(.)/i", "\${1} ", $bad_words);
    
    

    As far as I can see, all this does is add a space after every character in the $bad_words string. What is the point in that?

  • On line #139: If you want a RegExp to match one of two variables, you can not include spaces in the pattern. A space is a character and will cause the RegExp to try and find a space in the input string.

Was This Post Helpful? 0
  • +
  • -

#3 shebby  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 29-June 11

Re: Preg Replace Error

Posted 21 December 2011 - 07:12 AM

View PostAtli, on 21 December 2011 - 04:31 AM, said:

  • What data are you getting from the database? Clearly something in the data is not right, because the line triggering the error is complaining about a missing parentheses, but you have no parentheses in there; only the data from the database.

  • What is this supposed to accomplish?
    preg_replace("/(.)/i", "\${1} ", $bad_words);
    
    

    As far as I can see, all this does is add a space after every character in the $bad_words string. What is the point in that?

  • On line #139: If you want a RegExp to match one of two variables, you can not include spaces in the pattern. A space is a character and will cause the RegExp to try and find a space in the input string.


To answer your question

1.Posted Image - This database

2. This point for example im want to banned word suck it can banned word suck whether in capital letter or small letter but when comes to s u c k there is white space right, so that why I am using this rule

3. Iam had redo it

 function filterBadWords($str)
 {
    $result = mysql_query("SELECT badwords FROM clean") or die(mysql_error()); 
    $replacements = "*";
    
    while($row = mysql_fetch_assoc($result))
    {     
          $bad_words= $row['badwords'];
          $spaced_bad_words = preg_replace("/(.)/i", "\${1} ", $bad_words);
          $spaced_bad_words = trim($spaced_bad_words);
          $str = preg_replace("/$bad_words|$spaced_bad_words/i", str_repeat('*', strlen($bad_words)), $str);
    }

    return $str;
}


without space between preg replace still same error im wonder why?
Was This Post Helpful? 0
  • +
  • -

#4 shebby  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 29-June 11

Re: Preg Replace Error

Posted 21 December 2011 - 07:26 AM

Actually Iam trying converting from the array ones to the database in which earlier i had coded it

<?php
function filterBadWords($str) {
    
    $replacements = "*";
    $bad_words_array = array("chit","poop","shat","chits");
    
    foreach($bad_words_array as $bad_words) {
    $spaced_bad_words = preg_replace("/(.)/i", "\${1} ", $bad_words);
    $spaced_bad_words = trim($spaced_bad_words);
    
          $str = preg_replace("/$bad_words|$spaced_bad_words/i", str_repeat('*', strlen($bad_words)), $str);
    }  
    
    return $str;
}

$text = "I have to deal with a whole pile of chit dealing with you. Even C H I T will work too.";
echo filterBadWords($text);
?>


The results is "I have to deal with a whole pile of ****dealing with you. Even **** will work too."

The array ones is worked but for the database it cannot worked and got error
Was This Post Helpful? 0
  • +
  • -

#5 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

Re: Preg Replace Error

Posted 21 December 2011 - 09:44 AM

Well if it works with you putting them in an array like that, simply select the results into an array.

$bad_words_array = array();

$select = mysql_query("SELECT badwords FROM clean") or die(mysql_error());

while($row = mysql_fetch_assoc($select))
{

$bad_words_array[] = $row['badwords'];

}


// Then just do what you did previously, with that array



Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,088
  • Joined: 08-August 08

Re: Preg Replace Error

Posted 21 December 2011 - 11:57 AM

Might as well get the spaced words in there too.
$bad_words_array = array();
$select = mysql_query("SELECT badwords FROM clean") or die(mysql_error());
while($row = mysql_fetch_assoc($select)) {
	$bad_words_array[] = $row['badwords'];
	$bad_words_array[] = preg_replace("/(.)/i", "\${1} ", $row['badwords']);
}


Was This Post Helpful? 0
  • +
  • -

#7 shebby  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 29-June 11

Re: Preg Replace Error

Posted 22 December 2011 - 10:18 AM

View PostCTphpnwb, on 21 December 2011 - 11:57 AM, said:

Might as well get the spaced words in there too.
$bad_words_array = array();
$select = mysql_query("SELECT badwords FROM clean") or die(mysql_error());
while($row = mysql_fetch_assoc($select)) {
	$bad_words_array[] = $row['badwords'];
	$bad_words_array[] = preg_replace("/(.)/i", "\${1} ", $row['badwords']);
}



but without using this " $str = preg_replace("/$bad_words|$spaced_bad_words/i", str_repeat('*', strlen($bad_words)), $str);"

it would not filter my bad words for example suck it can be appear in chatlog


View PostDuckington, on 21 December 2011 - 09:44 AM, said:

Well if it works with you putting them in an array like that, simply select the results into an array.

$bad_words_array = array();

$select = mysql_query("SELECT badwords FROM clean") or die(mysql_error());

while($row = mysql_fetch_assoc($select))
{

$bad_words_array[] = $row['badwords'];

}


// Then just do what you did previously, with that array




but my function return str

function filterBadWords($str) so where the str going to be replace in that function because if im return array it gives an error
Was This Post Helpful? 0
  • +
  • -

#8 shebby  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 29-June 11

Re: Preg Replace Error

Posted 22 December 2011 - 10:53 AM

This is a the new ones im doing

 function filterBadWords($str)
 {
    $result = mysql_query("SELECT badwords FROM clean") or die(mysql_error());
    $replacements = "*";
    
    while($row = mysql_fetch_assoc($result))
    {     
          $bad_words_array = array($row['badwords']);
          $spaced_bad_words = preg_replace("/(.)/i", "\${1} ", $bad_words_array);
          $spaced_bad_words = trim($spaced_bad_words);
          $str = preg_replace("/$bad_words_array|$spaced_bad_words/i", str_repeat('*', strlen($bad_words_array)), $str);
    }

    return  $str;
}



no errors as the output is showing only it does not filter the words in the database :( im don know why
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,088
  • Joined: 08-August 08

Re: Preg Replace Error

Posted 22 December 2011 - 01:24 PM

View Postshebby, on 22 December 2011 - 01:18 PM, said:

but without using this " $str = preg_replace("/$bad_words|$spaced_bad_words/i", str_repeat('*', strlen($bad_words)), $str);"

it would not filter my bad words for example suck it can be appear in chatlog

You need to reread my post. It does use them. ;)
Was This Post Helpful? 0
  • +
  • -

#10 shebby  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 29-June 11

Re: Preg Replace Error

Posted 22 December 2011 - 09:28 PM

View PostCTphpnwb, on 21 December 2011 - 11:57 AM, said:

Might as well get the spaced words in there too.
$bad_words_array = array();
$select = mysql_query("SELECT badwords FROM clean") or die(mysql_error());
while($row = mysql_fetch_assoc($select)) {
	$bad_words_array[] = $row['badwords'];
	$bad_words_array[] = preg_replace("/(.)/i", "\${1} ", $row['badwords']);
}



function filterBadWords($str) then what should be replaced with this str?

and where is the replacements for * how can it ban the words using that asterisks *
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,088
  • Joined: 08-August 08

Re: Preg Replace Error

Posted 22 December 2011 - 10:14 PM

Try this:
<?php
$retrieved_from_db = array("bass", "luck", "witch");
$badwords = array();
foreach($retrieved_from_db as $row['badwords']) { // simulating your while loop, which retrieves from db.
	$badwords[] = $row['badwords'];
	$badwords[] = preg_replace("/(.)/i", "\${1} ", $row['badwords']);
}
$test = "Kiss my bass b a s s you lucky witch!";
$test = str_replace($badwords, "#@*", $test);
echo $test;
?>


This post has been edited by CTphpnwb: 22 December 2011 - 10:16 PM

Was This Post Helpful? 0
  • +
  • -

#12 shebby  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 29-June 11

Re: Preg Replace Error

Posted 23 December 2011 - 12:14 AM

View PostCTphpnwb, on 22 December 2011 - 10:14 PM, said:

Try this:
<?php
$retrieved_from_db = array("bass", "luck", "witch");
$badwords = array();
foreach($retrieved_from_db as $row['badwords']) { // simulating your while loop, which retrieves from db.
	$badwords[] = $row['badwords'];
	$badwords[] = preg_replace("/(.)/i", "\${1} ", $row['badwords']);
}
$test = "Kiss my bass b a s s you lucky witch!";
$test = str_replace($badwords, "#@*", $test);
echo $test;
?>




Sir this is my full code

<html>
<head>
<script type="text/javascript">

var refreshPeriod = 120; // 120 Seconds

            function refresh()
            {
                document.cookie = 'scrollTop=' + filterScrollTop();
                document.cookie = 'scrollLeft=' + filterScrollLeft();
                document.location.reload(true);
            }

            function getCookie(name)
            {
                var start = document.cookie.indexOf(name + "=");
                var len = start + name.length + 1;

                if(((!start) && (name != document.cookie.substring(0, name.length))) || start == -1)
                    return null;

                var end = document.cookie.indexOf(";", len);

                if(end == -1)
                    end = document.cookie.length;

                return unescape(document.cookie.substring(len, end));
            }

            function deleteCookie(name)
            {
                document.cookie = name + "=" + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
            }

            function setupRefresh()
            {
                var scrollTop = getCookie("scrollTop");
                var scrollLeft = getCookie("scrollLeft");

                if (!isNaN(scrollTop))
                {
                    document.body.scrollTop = scrollTop;
                    document.documentElement.scrollTop = scrollTop;
                }

                if (!isNaN(scrollLeft))
                {
                    document.body.scrollLeft = scrollLeft;
                    document.documentElement.scrollLeft = scrollLeft;
                }

                deleteCookie("scrollTop");
                deleteCookie("scrollLeft");

                setTimeout("refresh()", 2000);
            }

            function filterResults(win, docEl, body)
            {
                var result = win ? win : 0;

                if (docEl && (!result || (result > docEl)))
                    result = docEl;

                return body && (!result || (result > body)) ? body : result;
            }

            // Setting the cookie for vertical position
            function filterScrollTop()
            {
                var win = window.pageYOffset ? window.pageYOffset : 0;
                var docEl = document.documentElement ? document.documentElement.scrollTop : 0;
                var body = document.body ? document.body.scrollTop : 0;
                return filterResults(win, docEl, body);
            }

            // Setting the cookie for horizontal position
            function filterScrollLeft()
            {
                var win = window.pageXOffset ? window.pageXOffset : 0;
                var docEl = document.documentElement ? document.documentElement.scrollLeft : 0;
                var body = document.body ? document.body.scrollLeft : 0;
                return filterResults(win, docEl, body);
            }


</script>
</head>
<body onload="setupRefresh()">
<?php $db = mysql_connect("localhost", "root") or die("Could not connect."); //username and password
mysql_select_db("cute") or die(mysql_error());

$getnummessages="SELECT COUNT(*) as messagecount from chatmessages";

$getnummessages2=mysql_query($getnummessages) or die("blah");

$getnummessages3= mysql_result($getnummessages2, 0);

if($getnummessages3>40)

{

   $startrow=$getnummessages3-20;

}

else

{

  $startrow=0;

}

$getmsg="SELECT name, message from chatmessages order by postime ASC limit $startrow,$getnummessages3";

$getmsg2=mysql_query($getmsg) or die(mysql_error());

function replacesmiley($msg)
{
         $msg = str_replace(':)/>', '<img src="smile.gif" alt=":)/>" />', $msg);
         $msg = str_replace(':P/>', '<img src="p.gif" alt=":P/>" />', $msg);
         $msg = str_replace('*lol*', '<img src="lol.gif" alt="*lol*" />', $msg);
         $msg = str_replace('*blush*', '<img src="blush.gif" alt="*blush*" />', $msg);
         $msg = str_replace(':@', '<img src="angry.gif" alt=":@" />', $msg);
         return $msg;
}

echo replacesmiley($text);

 function filterBadWords($str)
 {
    $result = mysql_query("SELECT badwords FROM clean") or die(mysql_error());
    $replacements = "*";
    
    while($row = mysql_fetch_assoc($result))
    {     
          $bad_words_array = array($row['badwords']);
          foreach($bad_words_array as $bad_words)
          {
          $spaced_bad_words = preg_replace("/(.)/i", "\${1} ", $bad_words);
          $spaced_bad_words = trim($spaced_bad_words);
          $str = preg_replace("/$bad_words|$spaced_bad_words/i", str_repeat('*', strlen($bad_words)), $str);
          }
    }

    return  $str;
}



while($getmsg3=mysql_fetch_array($getmsg2))

{

  $getmsg3['message'] = replacesmiley($getmsg3['message']);
  $getmsg3['message'] = filterBadWords($getmsg3['message']); //Smiley faces
  print "<hr><font color='blue'><b>$getmsg3[name]:</b></font> $getmsg3[message]<br>";



}






?>

</body>
</html>





but in your answer coding there is no function so im decide to show you the full code because still im confused where to return because in the filter words im need to return sir :)
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,088
  • Joined: 08-August 08

Re: Preg Replace Error

Posted 23 December 2011 - 07:42 AM

So turn it into a function:
function filterBadWords($str) {
	$retrieved_from_db = array("bass", "luck", "witch"); // Used for simulating your while loop, which retrieves from db.
	$badwords = array();
	foreach($retrieved_from_db as $row['badwords']) { // simulating your while loop, which retrieves from db.
		$badwords[] = $row['badwords'];
		$badwords[] = preg_replace("/(.)/i", "\${1} ", $row['badwords']);
	}
	return str_replace($badwords, "#@*", $str);
}


By the way, your replacesmiley() function would be easier to read/edit this way:
function replacesmiley($msg)
{
	 $replace = array(':)/>', ':P/>', '*lol*', '*blush*', ':@');
	 $with = array('<img src="smile.gif" alt=":)/>" />', '<img src="p.gif" alt=":P/>" />', '<img src="lol.gif" alt="*lol*" />', '<img src="blush.gif" alt="*blush*" />', '<img src="angry.gif" alt=":@" />');
	 return str_replace($replace, $with, $msg);
}


Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,088
  • Joined: 08-August 08

Re: Preg Replace Error

Posted 23 December 2011 - 07:49 AM

Now I have a few questions:
Why do you think we have .html, .js, and .php files, and why are you only using one of them?
Have you run into the "headers already sent" problem yet?
Do you get confused as to which language you're reading when debugging your code? I do.

Read: http://www.dreaminco...ode-separation/
Was This Post Helpful? 0
  • +
  • -

#15 shebby  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 29-June 11

Re: Preg Replace Error

Posted 23 December 2011 - 08:01 AM

 function filterBadWords($str)
 {
    $result = mysql_query("SELECT badwords FROM clean") or die(mysql_error());
    $replacements = "*";
    
    while($row = mysql_fetch_assoc($result))
    {     
          $badwords[] = $row['badwords'];
          $badwords[] = preg_replace("/(.)/i", "\${1} ", $row['badwords']);
          $str = preg_replace("/$bad_words|$spaced_bad_words/i", str_repeat('*', strlen($bad_words)), $str);
    }

    return str_replace($badwords, $replacements, $str);

}



thanks again for kindly cooperate with me iam had done this code and this code can be run but when iam put suck word its only put * it supposed to put **** im means according how many letter involved in suck word.Which wrong in that code?

View PostCTphpnwb, on 23 December 2011 - 07:49 AM, said:

Now I have a few questions:
Why do you think we have .html, .js, and .php files, and why are you only using one of them?
Have you run into the "headers already sent" problem yet?
Do you get confused as to which language you're reading when debugging your code? I do.

Read: http://www.dreaminco...ode-separation/



yup iam a bit worst in array but further more worst in english :(
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2