PDA

View Full Version : First X Characters of string


Erindesign
10-10-2007, 02:11 AM
Hey all,

I'm having a brain fart here. I just forgot the preset function for taking the first x characters of a string. example:

$foo="Hello World";

After taking the first 5 characters $foo is "Hello".

wordwrap? strlen? subtr? grrrrrrrrrrrrr
Someone remind me real quick. I tried searching on youtube but I don't even know what to search for... Really big brain fart.

_Aerospace_Eng_
10-10-2007, 02:22 AM
Why the heck would you search on youtube? Its substr
substr (http://us.php.net/substr)

Erindesign
10-10-2007, 02:26 AM
LOL. I can't believe I said youtube. Maybe beacause my mind is there. I'm waiting for the New Youtube/Adsense Video Ads approval from Youtube. I typed that instead.

Good question. I laughed too. lol.


edit: Uhm, I don't want to subtract a certain number of characters, I want to echo a certain number of characters. I could do it via wordwrap and then explode, but I want a better way, so heres the example for better understanding:

$foo="Erindesign is noob at php. He doesn't know simple functions";
$foo1=wordwrap($foo, 50, "||");
$foo2=$explode("||", $foo1);
$result=$foo2[0];


$result would be what I'm looking for. It's the first 50 characters all together. Keep in mind, I don't know the total length of the string.

_Aerospace_Eng_
10-10-2007, 02:36 AM
Umm...I don't think you read the documentation to well. This gets you what you want
<?php
$foo="Erindesign is noob at php. He doesn't know simple functions";
echo substr($foo,0,50);
?>
substr does not mean "subtract'.

djames
10-10-2007, 06:50 AM
Umm...I don't think you read the documentation to well. This gets you what you want
<?php
$foo="Erindesign is noob at php. He doesn't know simple functions";
echo substr($foo,0,50);
?>
substr does not mean "subtract'.

lol... I love it.