php - Filter multiple strings out of string -


i've been working on code , got pint works php processes wondering if there easier ways fix problem

the database i'm working filled tracks artists, , example table:

  • composer_name = [@123:artist_display_name1] ft. [@58:artist_display_name2]
  • track_title = le example ( [@798:artist_display_name3] remix)

where number between [ : artist_id (linked artist profile) , string between : ] display name artist (sometimes artists use different names, that's why)

now question how display names fast possible without brackets, use artist_id perform action (such making link out of , put artist_id in database or something)

the obligatory way use regex preg_match:

function renderrow($rawrow) {     return preg_replace("/\[@([0-9]*):([^\]]*)\]/", "$2", $rawrow); } 

another way 10x 20x times faster (according quick benchmarks) more direct approach (o(n)):

function renderrow($rawrow) {     $len = strlen($rawrow);     $status = 0;     ($i = 0; $i < $len; $i++) {         $char = $rawrow[$i];         if ($char === '[')             $status = 1;         else if ($status === 1) {             if ($char === ':')                 $status = 2;             continue;         }         else if ($status === 2 && $char === ']')             $status = 0;         else              $row .= $char;     }     return $row; } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -