Jump to content


Photo

function


  • Please log in to reply
3 replies to this topic

#1 desithugg

desithugg
  • Members
  • PipPipPip
  • Advanced Member
  • 281 posts
  • LocationScarborough

Posted 07 October 2006 - 11:44 PM

<?php
$show1 = "3232sad4.32342";
function reverse_strrchr($haystack, $needle)
{
   $pos = strrpos($haystack, $needle);
   if($pos === false) {
       return $haystack;
   }
   return substr($haystack, 0, $pos + 1);
}
$show = reverse_strrchr($show1, '.');
echo $show;
?>
the code aove would echo 3232sad4. (everything before the .)
but if i want to echo everything after the . what should i do?

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 08 October 2006 - 12:12 AM

<?php
$show1 = "3232sad4.32342";

list($before, $after) = explode('.', $show1);
?>

or

<?php
$show1 = "3232sad4.32342";

$parts = explode('.', $show1);

$before = $parts[0];
$after = $parts[1];
?>

or

<?php
$show1 = "3232sad4.32342";

$dotposition = strpos($show1, '.');

$after = substr($show1, $dotposition + 1); 
?>

there are plenty more ways but these should get you going
hope it helps
Cheers
Mark
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 08 October 2006 - 12:25 AM

Just a another method....

<?php

$show1 = "3232sad4.32342";

function reverse_strrchr ( $str, $find, $type = 0, $include = false )
{
	if ( ( $pos = strrpos ( $str, $find ) ) !== false )
	{
		return ( $type == 1 ? $include ? substr ( $str, $pos ) : substr ( $str, $pos + 1 ) : substr ( $str, 0, ( $include ? $pos + 1 : $pos ) ) );
	}

	return ( $str );
}

/*
*
* reverse_strrchr ( param_1, param_2, $param_3, param_4 )
*
* param_1 = haystack
* param_2 = needle
* param_3 = 0 -> return left side of needle, 1 -> return right side of needle
* param_4 = BOOL return with needle -> true (include needle), false (don't include needle)
*
*/

// substring start of string (don't include 'needle')

echo reverse_strrchr ( $show1, '.' );

echo '<br />';

// substring start of string (include 'needle')

echo reverse_strrchr ( $show1, '.', 0, true );

echo '<br />';

// substring end of string (dont include 'needle')

echo reverse_strrchr ( $show1, '.', 1 );

echo '<br />';

// substring end of string (include 'needle')

echo reverse_strrchr ( $show1, '.', 1, true );

echo '<br />';

?>
[code]


me![/code]


#4 JasonLewis

JasonLewis
  • Members
  • PipPipPip
  • Advanced Member
  • 3,351 posts
  • LocationVictoria, Australia

Posted 08 October 2006 - 01:41 AM

why write huge lines of code when u can use the explode and list method, whcih only takes one line. so much more simplier.
Good luck with your coding.
Jason / ProjectFear / Jaysonic




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users