andy
andy

Reputation: 459

Retrieve Numbers After Certain Part of String

I know there are a couple of posts on here but I can't quite apply them to what I am looking for. I have a column in the database that is called UF_CRM_TASK. It contains differing values that look like:

a:2:{i:0;s:3:"C_8";i:1;s:4:"CO_6";}

SELECT UF_CRM_TASK FROM b_uts_tasks_task

I need the number that always follows CO_. Sometimes the number might be 1,2,3,4 or 5 figures long. So for example CO_22 or CO_348 or CO_8374

I need to echo the out in PHP. If somebody could point me in the right direction that would be great.

Upvotes: 0

Views: 63

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269873

You can actually do this using substring_index() and silent conversion:

select substring_index(UF_CRM_TASK, 'CO_', -1) + 0

Here is a SQL Fiddle.

Upvotes: 1

undefined_variable
undefined_variable

Reputation: 6218

<?php
$a = 'a:2:{i:0;s:3:"C_8";i:1;s:4:"CO_6";}';
$pos  = strpos($a,'CO_');
$pos1 = strpos($a,'"',$pos);
$len = $pos1-($pos+3);
$str = substr($a,$pos+3,$len);
echo $str;
?>

$a will be the value you fetch from database this can help

Upvotes: 0

Related Questions