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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.