Reputation: 35
I’m working on a PHP script in which the format one of the strings I’m getting as input is unknown. For example, I may get the input:
user Name <7a240011-1b54-4a91-be27-a5cf8f474a39
Or I could get the input:
7a240011-1b54-4a91-be27-a5cf8f474a39
My issue if the string comes in like this:
user Name <7a240011-1b54-4a91-be27-a5cf8f474a39
How do I remove user Name <
so all I’m left with is the UUID?
Now, what’s making this even harder is the UUID is never the same and
user Name <
will always have a different name.
Upvotes: 1
Views: 2019
Reputation: 674
the easiest solution is that you can just strip off everything before the first ocurrence of '<'. i've substituted 'name' here with two different sample names to illustrate:
<?php
$name1 = "user Michael <7a240011-1b54-4a91-be27-a5cf8f474a39";
$name2 = "user Jasvinder <7a240011-1b54-4a91-be27-a5cf8f474a39";
print substr($name1, strpos($name1, "<")+1);
print substr($name2, strpos($name2, "<")+1);
note, though, that if the "Name" field contains "<", there will be difficulties.
edit now that you've accepted this as the solution, id' like to change it something better:
<?php
$name1 = "user Michael <7a240011-1b54-4a91-be27-a5cf8f474a39";
$name2 = "user Jasvinder <7a240011-1b54-4a91-be27-a5cf8f474a39";
$name3 = "7a240011-1b54-4a91-be27-a5cf8f474a39";
print preg_replace("/^.*\</","",$name1);
print preg_replace("/^.*\</","",$name2);
print preg_replace("/^.*\</","",$name3);
this handles both cases which the previous one, as was pointed out, didn't
Upvotes: 0
Reputation: 116110
You can easily do this with strpos
and substr
. strpos
returns the index of a character or false
if it is not found. That way, you can leave the string untouched if it doesn't contain an <
.
Note that you need a strict comparison operator (===
or !==
) when checking for false
, because the string may contain a <
in position 0
.=, which would also evaluate to false (or vice versa) when compared using normal comparison operators (==
, !=
).
<?php
$x = 'user Name <7a240011-1b54-4a91-be27-a5cf8f474a39';
// Find the position of the `<`.
$p = strpos($x, '<');
// If that character exists, copy only the part after it.
if ($p !== false) {
$x = substr($x, $p+1);
}
echo $x;
You can easily make a function out of this so you can call it from anywhere. And as you can see it handles cases for different user names, or just a code without a user name at all:
<?php
function getUserId($input) {
$p = strpos($input, '<');
if ($p !== false) {
$input = substr($input, $p+1);
}
return $input;
}
echo getUserId('user Name <7a240011-1b54-4a91-be27-a5cf8f474a39') . '<br>';
echo getUserId('user Longer Name <7a240011-1b54-4a91-be27-a5cf8f474a39') . '<br>';
echo getUserId('7a240011-1b54-4a91-be27-a5cf8f474a39') . '<br>'a;
Upvotes: 0
Reputation: 129001
That seems like one of the few legitimate uses of regular expressions. After writing a regular expression to match UUIDs (like this one), you can throw your string into preg_match
, and if there are any UUIDs in it, it will find them and you can extract them.
Upvotes: 1