Hi guys,

I accidently inserted " " into one column in mysql database. Column has one char length space as it is expected after insertion. However, in php code, I can not do this:

None of these returns Yes.

if(substr($data, 0, 1) == " "){
 echo "Yes";
}
if(substr($data, 0, 1) == " "){
 echo "Yes";
}
if(substr($data, 0, 1) == ""){
 echo "Yes";
}

Only this return Yes. This is not efficient because any of a,b,c,t,w,g,u,o,h ... could be yes as well.

if(is_string(substr($data, 0, 1))){
 echo "Yes";	
}

Please help
Thanks in advance

Hi veledrom,

I did not understand your question. Could you please rephrase it?

Best Regards,

- Pedro Rubini

if(stripos('cc'.$data,' '))
{
    echo 'yes';
}

//cc or any text is added in front because
//stripos($data,' ') will always return 0 ie. position of the string

Hi Pedro,

This is what I Post to next script:

<input type="text" name="mytext" value="<?php echo "&nbsp;"; ?>" />

In the next script:
Insert $_POST["mytext"] into a table in database.

When you check the relevant column in database, obviously you will see a space(1 char length) for it.

Now how it is done to check whether it is a space or not. Three examples I showed in first threat doesn't work.

Thanks

Member Avatar for Zagga

Hi veledrom,

Try searching for the HTML character entity for space.

if ($_POST["mytext"] == "&# 32;"{       // remove the space between &# 32.  I inserted the space so it would show.
echo "Is a space";
}
else{
echo "Is NOT a space";
}

Zagga
p.s. I didn't read your other post so I don't know what has been suggested before. (A good reason to not double post).

&nbsp; is actually 6 characters not one.
use this

$data = str_replace('&nbsp;', ' ', $data)
if(substr($data, 0, 1) == " ")
{
 echo "Yes";
}
if(substr($data, 0, 1) == "")
{
echo "Yes";
}

If the &nbsp; was in a text field being returned as post var (it is a single character)

try this

$nbsp = chr(160); // the dec value for a HTML &nbsp;
$data = str_replace($nbsp, " ", $data) // will turn the &nbsp; into a standard space

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.