• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

PHP - Newbi variable question

XeonGX

New Member
Hello everyone,
I was wondering if there is a way to use only part of a php variable.
For example, if
<?php
$word = 'word1242';
echo $word;
?>
the output should be 1242, instead of word1242.
PS I've got a really long list, so I can't really change the $word to 1242.

Any help would be much appreciated. :)
 
in the list that you have, is "word" the thing you want to take off all of them ??
I dont really want to change the list, because its also used for something else.
I was looking more for a code when outputing the code.
 
So "word" is the string you want to remove from each item in the list, but without actually editing the list ??

PHP:
foreach( array( 'word1234', 'word2345', 'word3456' ) as $word )
{
	printf( "%s<br />\n", substr( $word, 4 ) );
}

Draw your attention to

PHP:
substr( $word, 4 )
 
So "word" is the string you want to remove from each item in the list, but without actually editing the list ??

PHP:
foreach( array( 'word1234', 'word2345', 'word3456' ) as $word )
{
	printf( "%s<br />\n", substr( $word, 4 ) );
}

Draw your attention to

PHP:
substr( $word, 4 )

I just tried this, it doesn't seem to work. It would output word1234 word2345 word 3456.

EDIT:
I played around with some javascript codes, and got it to work. lol
Code:
<script language="JavaScript"><!--
var word="<? echo $word; ?>";
var num1=word.charAt(4);
var num2=word.charAt(5);
var num3=word.charAt(6);
var num4=word.charAt(7);
var num=num1 + num2 + num3 + num4;
document.write(num);
//--></script>
:)
 
Last edited:
If it's just and always the word, word you could:
PHP:
<?php
$array = array('word1234', 'word4321');
foreach($array as $word) {
$word2 = explode("word", $word);
echo $word2[0];
}
?>
 
Back
Top