Jump to content

function


desithugg

Recommended Posts

[code]
<?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;
?>
[/code]
the code aove would echo 3232sad4. (everything before the .)
but if i want to echo everything after the . what should i do?
Link to comment
https://forums.phpfreaks.com/topic/23317-function/
Share on other sites

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

list($before, $after) = explode('.', $show1);
?>
[/code]

or

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

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

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

or

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

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

$after = substr($show1, $dotposition + 1);
?>
[/code]

there are plenty more ways but these should get you going
hope it helps
Cheers
Mark
Link to comment
https://forums.phpfreaks.com/topic/23317-function/#findComment-105719
Share on other sites

Just a another method....

[code]
<?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][/code]
Link to comment
https://forums.phpfreaks.com/topic/23317-function/#findComment-105722
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.