Question

I have spent a while to search around the internet but still couldn't find out why my script didn't work :(

Here is my string ($title) which I need to check

Nanatsu no Taizai – Chương 071: Tồn tại trong bóng đêm

And and list of regular expression I used but always returned false

"/^([a-z\ ]+)[\-]{1,}/i"

"/^([a-z\ ]+)[\ ]{0,1}[\-]{1,}/i"

"/^([a-z\ ]+)[\-]{1,}/ui"

And the code I used is

preg_match($regex,$title,$matches)

Please help me . Thank you in advance.

Était-ce utile?

La solution 2

Maybe this works for you

$t = "Nanatsu no Taizai - Chương 071: Tồn tại trong bóng đêm";

preg_match('/(.*) - (.* [0-9]{1,}:) (.*)/i', $t, $matches);

print_r($matches);

Result

Array
(
    [0] => Nanatsu no Taizai - Chương 071: Tồn tại trong bóng đêm
    [1] => Nanatsu no Taizai
    [2] => Chương 071:
    [3] => Tồn tại trong bóng đêm
)

Better?

Autres conseils

If you're just trying to extract everything up to the first -, the best way to do that is not to use a regular expression at all. strtok and trim, might be ideal for you:

$result = trim(strtok($title, '-'));

If you want the result to include the - then, you can do:

$result = strtok($title, '-') . '-';

Take a look at Unicode character properties, find your language from the table and use it like \p{Thai}

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top