How to Check for Special Characters PHP

how to check for special characters php

<?php

$string = 'foo';

if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $string))
{
// one or more of the 'special characters' found in $string
}

How to check for white spaces and special characters

Considering the error message you wrote, you are complicating yourself.

Instead of searching for the list of characters that aren't alpha num, search for alpha num only. Try using this pattern, and don't negate the condition.

$pattern = "/^[a-zA-Z0-9]+$/";
// Some code...
if(preg_match($pattern, $usern))
// ^-------------------------------Notice the changes
{
//Username is valid
}

Description of the pattern :

^ from the begining

[a-zA-Z0-9] search an alpha num

+ 1 or more time

$ to the end

/^[a-zA-Z0-9]+$/can be replaced by /^[[:alnum:]]+$/ or /^[a-z\d]+$/i which produce the same effect.

Find a substring inside a string with special characters PHP

This will work if idIwant has only numbers.

$string = '{},\"employees\":{},\"idIwant\":{\"2545\":{\"attributes\":{\"offset\":9855,';

preg_match('/idIwant.*?(\d+)/', $string, $matches);

echo $matches[1];

Test

Getting PHP code to recognize special characters

Special thanks to user @dorad for helping me get this working correctly. This is the code that actually works. I suspect the original version I had was coded for an earlier version of PHP which will not work correctly for special characters in newer PHP versions. So without further ado, this is the final code that works:

<?php
//creates a image handle
$image = imagecreate( 700, 70 );

if(!empty($_POST["bgcolor"])){
$background = imagecolorallocate( $image,0, 0, 0);
}

else {
$background = imagecolorallocate( $image,255, 255, 255);
}

//GET COLORS FROM POST AND SPLIT INTO RGB FORMAT
$color = $_POST["color"];
$pieces = explode("-", $color);
//COLORS
$color = imagecolorallocate($image, hexdec("0x".$pieces[0].""), hexdec("0x".$pieces[1].""), hexdec("0x".$pieces[2].""));

$font = 'fonts/'.$_POST["font"].'';
$fontSize = "25";
$fontRotation = "0";
$str = utf8_encode_mix($_POST["name"]);

$regex = $str;
$replaced = preg_replace($regex,"%26",$str);

$tb = imagettfbbox(25, 0, $font, $str);
$x = ceil((700 - $tb[2]) / 2);

ImageTTFText($image, $fontSize, $fontRotation, $x, 50, $color, $font, $str);

header("Content-Type: image/PNG");
ImagePng ($image);
imagedestroy($image);

function utf8_encode_mix($input, $encode_keys=false)
{
if(is_array($input))
{
$result = array();
foreach($input as $k => $v)
{
$key = ($encode_keys)? utf8_encode($k) : $k;
$result[$key] = utf8_encode_mix( $v, $encode_keys);
}
}
else
{
$result = utf8_encode($input);
}

return $result;
}
?>

Calling page index2.php

<div style="padding:00px;">

<strong>Please complete this form to preview the font and color font your name Embroidery: </strong>
<br><br>

<form action="index2.php?page=embroidery" method="post" name="font" style="background-color:#EDFAFC;">

<input name="page" type="hidden" value="lab_coats_embroidery">
<?
function checkcolor ($color) {

if($color == $_POST["color"]){
echo "checked";
}
}

//
?>

<table width="600" border="0" cellspacing="4" cellpadding="4">
<tr>
<td width="163">Please enter your name: </td>
<td width="398">Line 1:
<input name="name" type="text" value="<?=$_POST["name"]?>" size="40">
<br />
Line 2:
<input name="name2" type="text" value="<?=$_POST["name2"]?>" size="40" />
<br />
Line 3:
<input name="name3" type="text" value="<?=$_POST["name3"]?>" size="40" /></td>
<td width="18"> </td>
<td width="21"> </td>
</tr>
<tr>
<td>Please select a font: </td>
<td><select name="font" id="font">
<option value="<?=$_POST["font"]?>"></option>
<option value="brush">Brush script size 14</option>
<option value="athletic">Athletic swoosh</option>
<!--<option value="diana">Diana script</option>-->
<option value="brush738">Brush 738</option>
<option value="helvetica">Helvetica narrow</option>
<option value="homeward">Homeward</option>
<option value="cheltenham">Cheltenham</option>
<option value="athletic_narrow">Athletic Narrow</option>
<option value="courier">Courier</option>
<?php /*?> <option value="cancun">Cancun</option><?php */?>
</select>
Color:
<select name="color" id="color">
<option value="<?=$_POST["color"]?>"></option>
<option value="00-00-00">Black</option>
<option value="cc-00-00">Red</option>
<option value="05-34-92">Royal Blue</option>
<option value="13-2c-61">Navy</option>
<option value="86-00-41">Burgundy</option>
<option value="fd-08-c8">Fuchsia</option>
<option value="73-b9-ff">Sky Blue</option>
<option value="02-74-8c">Teal</option>
<option value="02-4b-2d">Forest Green</option>
<option value="43-07-71">Purple</option>
<option value="ff-c6-00">Gold</option>
<option value="dd-dd-dd">Silver</option>
<option value="96-54-31">Bronze</option>
</select></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="button" id="button" value="Preview My Name" /></td>
<td> </td>
<td> </td>
</tr>
</table>
</form>

<div style="text-align:center;">
<!-- orig setting height 65px and no background positioning -->
<div style="background-image:url('dpimage.php?name=<?=$_POST["name"]?>&font=<?=$_POST["font"]?>&color=<?=$_POST["color"]?>'); background-repeat:no; background-position: center -20px; width:700px; height:40px;"></div>
<div style="background-image:url('dpimage.php?name=<?=$_POST["name2"]?>&font=<?=$_POST["font"]?>&color=<?=$_POST["color"]?>'); background-repeat:no; background-position: center -20px; width:700px; height:40px;"></div>
<div style="background-image:url('dpimage.php?name=<?=$_POST["name3"]?>&font=<?=$_POST["font"]?>&color=<?=$_POST["color"]?> '); background-repeat:no; background-position: center -20px; width:700px; height:40px;"></div>

</div>

</div>

Check if string contains two word or special character with underscore between in PHP

You need to add special characters in your regular expression, for example:

$value = 'mytext_%';
preg_match('/^[a-z$&+,:;=?@#|\'<>.^*()%!-]+_[a-z$&+,:;=?@#|\'<>.^*()%!-]+$/i', $value);

Determine the position of a special character in the string in PHP

<?php

$content = 'E77eF/74/VA';
//With this pattern you found everything except 0-9a-zA-Z
$pattern = "/[_a-z0-9-]/i";
$new_content = '';

for($i = 0; $i < strlen($content); $i++) {
//if you found the 'special character' then replace with the position
if(!preg_match($pattern, $content[$i])) {
$new_content .= $i + 1;
} else {
//if there is no 'special character' then use the character
$new_content .= $content[$i];
}
}

print_r($new_content);

?>

Output:

E77eF6749VA

Check if a strings has special characters except hyphen?

One option uses preg_match with the pattern [^A-Za-z0-9-]:

$str1 = "what?";
if (preg_match("/[^A-Za-z0-9-]/", $str1)) {
echo "YES";
}

This would print YES should there be at least one character in $str1 which is not alphanumeric or hyphen.

strpos function not working with special character

is a multi byte character and strpos cannot understand this.

What you are looking for is mb_strpos. This function performs the same action, but is multi-byte characters safe.


Update: While the answer above is correct, it was not the entire solution to your case. The problem is you are making the if check too complicated.

You are first inverting the result of strpos and then checking if it does not match false. I don't think this was your intention.
To fix this, just check if the result of mb_strpos equals false.

if (mb_strpos($name_to_check, '★ StatTrak™') === false)
echo 'not contain';
} else {
echo 'contain';
}


Related Topics



Leave a reply



Submit