NoNameZ
NoNameZ

Reputation: 795

Get value between given

I need to get all users-ids without user prefix, so i trying to get it between dot and end of word.

$string = 'users.user_id1, users.user_id2, users.user_id3, users.user_id4';
preg_match_all('/\.(.*?)\/B/', $string, $matches);

Upvotes: 0

Views: 78

Answers (4)

Zathrus Writer
Zathrus Writer

Reputation: 4331

try this:

$string = 'users.user_id1, users.user_id2, users.user_id3, users.user_id4';
preg_match_all('/users.([^,]+),?/', $string, $matches);

Upvotes: 2

xdazz
xdazz

Reputation: 160883

Use

preg_match_all('/(?<=\.)[^,]+(?=,|$)/', $string, $matches);

But if you the prefix is fixed, you could use string replace function for the needs.

Upvotes: 1

Tarun
Tarun

Reputation: 3165

str_ireplace('users.','',str_ireplace(' ','',$string))

It will take care of spaces too Thanks

Upvotes: 1

Zerium
Zerium

Reputation: 17333

Try:

$string = 'users.user_id1, users.user_id2, users.user_id3, users.user_id4';
$temparray1 = explode(", ", $string);
$temparray2 = array();
foreach ($temparray1 as $value) {
$temp = explode(".", $value);
$temparray2[] = $temp[1];
}
$finalstring = implode(", ", $temparray2);
unset($temparray2);
unset($temparray1);

$finalstring is the content. It should be: user_id1, user_id2, user_id3, user_id4.

Tell me if you need it in another format.

UPDATE: Str_replace is also a possibility. Write:

$string = 'users.user_id1, users.user_id2, users.user_id3, users.user_id4';
$finalstring = str_replace('users.', '', $string);

Just isn't very versatile with the contents of the string.

Upvotes: 0

Related Questions