Strlen gives me the wrong count.

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 3729 Views - Last Post: 23 March 2011 - 01:10 PM Rate Topic: -----

#1 localhost  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 130
  • Joined: 13-December 10

Strlen gives me the wrong count.

Posted 22 March 2011 - 10:01 AM

Okay, I'm really stuck here.

My code is:

It isn't the whole code.
	require_once('config.php');
	
	$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
	if(!$link) {
		die('Failed to connect to server: ' . mysql_error());
	}
	
	$db = mysql_select_db(DB_DATABASE);
	if(!$db) {
		die("Unable to select database");
	}
$result = mysql_query("SELECT * FROM bedrijvenorders WHERE gebruikersnaam='$gebruikersnaam' AND ordernummer='$ordernummer'");
$row = mysql_fetch_array($result);
$tekst = $row['advertentietekst'];
echo strlen($tekst);



I want to show how many characters are in the string.

This is my string for example:
1
2
3
4
5



The strlen function returns: 13

And it must be: 9

How can I solve this problem??

This post has been edited by localhost: 22 March 2011 - 10:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Strlen gives me the wrong count.

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1866
  • View blog
  • Posts: 20,278
  • Joined: 17-March 01

Re: Strlen gives me the wrong count.

Posted 22 March 2011 - 10:04 AM

So wait, is your string literally:

1
2
3
4
5

1) Why would that return 9, wouldn't you want it to return 5?
2) There are /n characters you're not seeing if it's actually showing on separate lines that would be counted.
Was This Post Helpful? 0
  • +
  • -

#3 localhost  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 130
  • Joined: 13-December 10

Re: Strlen gives me the wrong count.

Posted 22 March 2011 - 10:07 AM

Uhm,

How do I explain....

I use an advert text. For example:

Hello!

This is a text!

Greetings.


Like if that was my advert text I want the amount of characters.
The text may only be 1000 characters long. If you enter it count as 1 character.
The text will be saved into the database.
Later you can edit the text.
I just call the text with mysql and use strlen to retrieve the amount of characters that were shown to you when you submitted the text.

I just want to get the original textlength, with enters etc...

I hope I explained enough.

This post has been edited by localhost: 22 March 2011 - 10:08 AM

Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1866
  • View blog
  • Posts: 20,278
  • Joined: 17-March 01

Re: Strlen gives me the wrong count.

Posted 22 March 2011 - 10:09 AM

You haven't given enough info as you say your string is:

1
2
3
4
5

And then you say this:

Quote

The strlen function returns: 13

And it must be: 9


1/n
2/n
3/n
4/n
5

That is 13. Which is correct.
Was This Post Helpful? 0
  • +
  • -

#5 localhost  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 130
  • Joined: 13-December 10

Re: Strlen gives me the wrong count.

Posted 22 March 2011 - 10:12 AM

Ah, I'm sorry...

Now I get it.
But the string:
1
2
3
4
5

is called into a textarea.
The /n aren't shown.
I don't want them to be counted aswell. I just want the characters and the enters/spaces be counted.

This post has been edited by localhost: 22 March 2011 - 10:13 AM

Was This Post Helpful? 0
  • +
  • -

#6 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1866
  • View blog
  • Posts: 20,278
  • Joined: 17-March 01

Re: Strlen gives me the wrong count.

Posted 22 March 2011 - 10:19 AM

Then you'll need to parse the string using a regex or function to strip those sorts of characters out. I'll let one of the PHP experts weigh on on the best way to do that.
Was This Post Helpful? 1
  • +
  • -

#7 localhost  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 130
  • Joined: 13-December 10

Re: Strlen gives me the wrong count.

Posted 22 March 2011 - 10:21 AM

Ok, Thank you!
Was This Post Helpful? 0
  • +
  • -

#8 lordspace  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-March 11

Re: Strlen gives me the wrong count.

Posted 22 March 2011 - 05:26 PM

what does echo mb_strlen($str); return ?

This post has been edited by lordspace: 22 March 2011 - 05:26 PM

Was This Post Helpful? 1
  • +
  • -

#9 localhost  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 130
  • Joined: 13-December 10

Re: Strlen gives me the wrong count.

Posted 23 March 2011 - 08:35 AM

Doesn't work either... Still returns: 13..... I have no idea how to fix this..
Skyhawk, you got an answer already??

In the post script for the text I use this code to get the characters. The limit is 1000.

Javascript:

<script language = "Javascript">

maxL=1000;
var bName = navigator.appName;
function advertentietekstLimit(taObj) {
	if (taObj.value.length==maxL) return false;
	return true;
}

function advertentietekstCount(taObj,Cnt) { 
	objCnt=createObject(Cnt);
	objVal=taObj.value;
	if (objVal.length>maxL) objVal=objVal.substring(0,maxL);
	if (objCnt) {
		if(bName == "Netscape"){	
			objCnt.textContent=0+objVal.length;}
		else{objCnt.innerText=0+objVal.length;}
	}
	return true;
}
function createObject(objId) {
	if (document.getElementById) return document.getElementById(objId);
	else if (document.layers) return eval("document." + objId);
	else if (document.all) return eval("document.all." + objId);
	else return eval("document." + objId);
}
</script>



Textbox:

      <td><center><textarea onkeypress="return advertentietekstLimit(this)" onkeyup="return advertentietekstCount(this,'advertentietekst')" cols="68" rows="15" maxlength="1000" name="advertentietekst" class="textfield"></textarea></center></td>
<td width='99'><SPAN id=advertentietekst> 0</SPAN> / 1000</td>



I hope this helps some more..

This post has been edited by localhost: 23 March 2011 - 08:31 AM

Was This Post Helpful? 0
  • +
  • -

#10 JaKWaC  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: Strlen gives me the wrong count.

Posted 23 March 2011 - 09:02 AM

Why do you think it should be 9?
Was This Post Helpful? 0
  • +
  • -

#11 localhost  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 130
  • Joined: 13-December 10

Re: Strlen gives me the wrong count.

Posted 23 March 2011 - 09:03 AM

Because the javascript on the post page says it 9 characters... And the strlen function 13...

It must be the same amount of characters as the post script..
Was This Post Helpful? 0
  • +
  • -

#12 JaKWaC  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: Strlen gives me the wrong count.

Posted 23 March 2011 - 09:08 AM

Alright so you want to count new lines as a single character. What about tabs, carriage returns, nul-bytes and vertical tabs?

The easiest way would be to just replace those characters with a single character and then get your count.
Was This Post Helpful? 0
  • +
  • -

#13 localhost  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 130
  • Joined: 13-December 10

Re: Strlen gives me the wrong count.

Posted 23 March 2011 - 09:09 AM

And how can I do that?
Was This Post Helpful? 0
  • +
  • -

#14 JaKWaC  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: Strlen gives me the wrong count.

Posted 23 March 2011 - 09:20 AM

In that code through this function in so we can check something:

function real_string($str)
{
	$search  = array("\t","\n","\r\n","\0","\v");
	$replace = array('\t','\n','\r\n','\0','\v');
	$str = str_replace($search,$replace,$str);
	
	return $str;
	

}


Run that function against the string you get from your form. This will tell us what exactly is in the string.

This post has been edited by JaKWaC: 23 March 2011 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

#15 localhost  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 130
  • Joined: 13-December 10

Re: Strlen gives me the wrong count.

Posted 23 March 2011 - 09:25 AM

I don't get it how to use it...
I barely understand javascript ;-)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2