Jump to content

extracting data from string


Ortix

Recommended Posts

so I'm working on this system which requires me to inject anime show names into a DB for an index.

 

I get them in this form:

[Kira-Fansub]_HIGHSCHOOL_OF_THE_DEAD_-_04_(BD_1920x1080_x264_AAC) [F0E73009].mkv

 

I need to get it in this form: Highschool Of The Dead

 

sometimes the words are space seperated.

the main part is to extract the text in between the ] and ( and with that i mean [stuff]extract this(other stuff) and sometimes it looks like this: [stuff]extract[stuff]

 

I have NO CLUE how to do this preg_match_all doesn't get me any further.. maybe in stages?

Link to comment
https://forums.phpfreaks.com/topic/222858-extracting-data-from-string/
Share on other sites

<?php
$name='[Kira-Fansub]_HIGHSCHOOL_OF_THE_DEAD_-_04_(BD_1920x1080_x264_AAC) [F0E73009].mkv';
$str=preg_replace(array('/(\s*?[\[(].*?[\])]\s*?)/','/(-.*)/','/(_)/','/(  )/'),array('','',' ',' '),$name);
echo $str;
?>

 

output

HIGHSCHOOL OF THE DEAD

How it works:

1st match ('/(\s*?[\[(].*?[\])]\s*?)/') catches [] and () items and removes them ('')

2nd match ('/(-.*)/') drops everything after the dash, including the dash ('')

3rd match (/(_)/) replaces underscores with spaces (' ')

4th match (/(  )/) Double spaces replaced with single space (' '):

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.