Link to home
Start Free TrialLog in
Avatar of iceman19330
iceman19330

asked on

Quick Question PHP - Strip String Pattern

I have a string pattern like
XCTY_MMYT_2222-1110-XX
or
ACDS_FFFFFFF-14XX-122121
and I want to strip everything from the last _ to the left so those would be

2222-1110-XX
or
FFFFFFF-14XX-122121

Thanks
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

You can find the position of the last underscore with this:
http://php.net/manual/en/function.strrpos.php

You can get the substring from that position to the end of the string with this:
http://php.net/manual/en/function.substr.php

I'll set up a test for you and show you the code.
ASKER CERTIFIED SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<?php // demo/temp_iceman19330.php
/**
 * http://www.experts-exchange.com/questions/28926349/Quick-Question-PHP-Strip-String-Pattern.html
 */
error_reporting(E_ALL);
echo '<pre>';

$delimiter = '_';

$testcases = array
( 'XCTY_MMYT_2222-1110-XX'
, 'ACDS_FFFFFFF-14XX-122121'
)
;
foreach ($testcases as $str)
{
    $dlm = strrpos($str, $delimiter) + 1;
    $new = substr($str, $dlm);

    echo PHP_EOL;
    echo $str;
    echo ' RESULT: ';
    echo $new;
}

Open in new window

Lemme know if you have any questions!
@Ray: I think in this case a regex solution is better.

Reasons:
1. very easy to understand string: ".*_" means "everything up to the last _"
2. it can be used for multi line input strings in one go, without changing anything:
$subject = "XCTY_MMYT_2222-1110-XX";
$result = preg_replace('/.*_/', '', $subject);
echo $result;

$subject = "XCTY_MMYT_2222-1110-XX
            ACDS_FFFFFFF-14XX-122121
            ABC_111-111-111";
$result = preg_replace('/.*_/', '', $subject);
echo $result;

Open in new window

My normal two cents would say that the str_ functions will always be faster than preg_, but strrpos is a slow beast (relatively-speaking), so it will run slower than preg_ in most scenarios.

Either way is lightning-quick, although the fastest way is in Dan's last post where he has all the values in one string and runs one preg_replace() call against it (virtually eliminating the overhead that comes from all the repeated function calls). So if the OP is looking to do this repeatedly on a massive scale (e.g. millions of entries), then his best bet is to read them all in as one string and do the single preg_replace on it. It'll be a couple seconds faster than the alternatives.

Probably none of this is actually relevant to the OP, but interesting info nonetheless (stupid strrpos!). :)
https://www.experts-exchange.com/articles/7830/A-Quick-Tour-of-Test-Driven-Development.html

The only reason I used an array was to facilitate testing.  My guess was that this data comes from some external source - a database, an API, a scanner, a form input - something like that, and that the inputs were arriving one-at-a-time.  If the number of transformations is less than several hundred per second, it may be hard to find much value in optimization.  Nothing against optimization, but one hour of debugging == a lot of computer cycles, and sometimes the lazy-easy code is a good way to go.  Premature optimization is usually considered an anti-practice.

(From random thoughts) has anybody tried benchmarking strrpos() against strrev() + strpos()?  It would be especially interesting to see the difference in a high-performance environment with PHP 7 or HHVM.
Avatar of iceman19330
iceman19330

ASKER

This worked really well on both the single and double underscore.
Thanks
You're welcome.

Glad I could help!