Help! Quote Problem and Uppercase-Lowercase letter trick in php

Started on the old forum in August 2011 · view the original in the archive · old links redirect here

#arrays #case #php #replace

5 replies · 662 views · started 15 years ago · 1 min read

4 participants
Sifat3d Developer · 66 posts #1

Please take a look at this simple php code:

<?php //string that needs to be customized $text = $_POST['text']; //placeholders array $placeholders = array('some', 'random', 'simple', 'text'); //replace values array $strangevals = array('die', 'in', 'this', 'tournament'); //strange string $strangestr = str_replace($placeholders, $strangevals, $text); echo "Weird text: ". $strangestr ; ?>

This receives some text from a html input form, replaces the placeholder arrays with the strangevals arrays. The problem is, when someone gives input for example: some random "burgers" and 'pizzas' It results in: die in "burgers"\ and 'pizza' How to solve the backward slash problem and show : die in "burgers" and 'pizza'

Please help me in this and provide sample code.

The Second thing: when someone inputs -some- its replaced with -die- but when someone enters -Some- or -soME- or -sOMe- its not replaced. I dont want to and cant create placeholders and strangevals for every single upper and lower case inputs like this! Is there any way around this? is there any way that if anyone inputs -some- or -SoMe- or -soME- or any other possible uppercase, lowercase combination its replaced with -die- ?

Please help me with this, im an newbie in php and really need help in this. 🙂

Fake hack any website,even coding-talk for some clean FUN!!! Try: fake hacking website Mig33 Software for pc: Mig33 for PC mobile games and software download

khan Lead · 681 posts #2

the first thing isnt a problem i guess.. as this is an array and those words are individual there shouldn't any problem with the " " commas. it will catch the word from it. and for the second thing you can use strtolower() function and check whether the input is replaceable or not. like this:

$input = strtolower($_POST['input']);
$finalinput = str_replace("$input","$replacetxt",$finalinput); #make your own array or function for text.
#now the whole text will show in lowercase.
kei_ki7 Senior · 431 posts #3

try to use preg_replace,

also read about this http://php.net/manual/en/reference.pcre.pattern.modifiers.php

for modifiers reference

Added after 5 minutes:

//strange string
$strangestr = str_replace($placeholders, $strangevals, $text);

try to use stripslashes in this problem by adding

$strangestr = stripslashes($strangestr);

Did I help you? You can help me too Your donations will help me finance my studies.

arnage Lead · 811 posts #5

Sifat3d wrote: Please take a look at this simple php code:

<?php //string that needs to be customized $text = $_POST['text']; //placeholders array $placeholders = array('some', 'random', 'simple', 'text'); //replace values array $strangevals = array('die', 'in', 'this', 'tournament'); //strange string $strangestr = str_replace($placeholders, $strangevals, $text); echo "Weird text: ". $strangestr ; ?>

First thing is that every input needs to be sanitized, which means html symbols converted into their entities. That can be done with htmlentities() and single and double quotes are converted with htmlentities()'s flag ENT_QUOTES, and than both qoutes aren't gonna be escaped with adding \ backslash. Example is:

$text = htmlentities($_POST['text'], ENT_QUOTES);

Read details about it here: PHP: htmlentities - Manual Second is done with one letter - i. "i" usualy means "insensetive case" and instead str_replace (string replace as is) use str_ireplace (string insensetive case raplce). Example is:

$strangestr = str_ireplace($placeholders, $strangevals, $text);

PHP: str_replace - Manual

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
Sifat3d Developer · 66 posts #6

arnage wrote: First thing is that every input needs to be sanitized, which means html symbols converted into their entities. That can be done with htmlentities() and single and double quotes are converted with htmlentities()'s flag ENT_QUOTES, and than both qoutes aren't gonna be escaped with adding \ backslash. Example is:

$text = htmlentities($_POST['text'], ENT_QUOTES);

Read details about it here: PHP: htmlentities - Manual Second is done with one letter - i. "i" usualy means "insensetive case" and instead str_replace (string replace as is) use str_ireplace (string insensetive case raplce). Example is:

$strangestr = str_ireplace($placeholders, $strangevals, $text);

PHP: str_replace - Manual

thanks that really helped.

Fake hack any website,even coding-talk for some clean FUN!!! Try: fake hacking website Mig33 Software for pc: Mig33 for PC mobile games and software download

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

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