1

I need a help to parse the characters inside those brackets:

  1. []
  2. {}
  3. <>
  4. {|}
  5. <|>

For example, I have this string variable (Japanese):

$question = "この<部屋|へや>[に]{椅子|いす}[が]ありません";

Expected result in HTML:

Description

  • 1) This is a particle. I will convert all word inside [] into HTML tag. Example: [に] will be converted into <span style="color:blue">に</span>. A full sentence can have multiple []. Note: I understand how to use str_replace.

  • 2 and 4) This is normal kanji word which will be used as a question to the user. A full sentence can only have one {}.

  • 3 and 5) This is normal kanji text. A full sentence can have multiple {}.

  • 2, 3, 4, and 5) They will converted into Ruby html tags. Sometimes they will not have a | separator, which is not mandatory. From what I understand, I just need to explode the | characters. If explode return false or | not exist, I will use original value. Note: I understand how to use ruby tags (rb and rt).

My question

How do I parse characters 1-5 I mentioned above with PHP? What keyword I need to start?

Thanks.

Toto
  • 89,455
  • 62
  • 89
  • 125
mokalovesoulmate
  • 271
  • 2
  • 6
  • 18

1 Answers1

0

Thanks to this page: Capturing text between square brackets in PHP, now I have my own answer.

Full code:

<?php
$text = "この<部屋|へや>[に]{椅子|いす}[が]ありません";
preg_match_all("/\[([^\]]*)\]/", $text, $square_brackets); //[]
preg_match_all("/{([^}]*)}/", $text, $curly_brackets); //{}
preg_match_all("/<([^}]*)>/", $text, $angle_brackets); //<>

print_r($square_brackets);
echo "\r\n";
print_r($curly_brackets);
echo "\r\n";
print_r($angle_brackets);
echo "\r\n";

Result:

Array
(
    [0] => Array
        (
            [0] => [に]
            [1] => [が]
        )

    [1] => Array
        (
            [0] => に
            [1] => が
        )

)

Array
(
    [0] => Array
        (
            [0] => {椅子|いす}
        )

    [1] => Array
        (
            [0] => 椅子|いす
        )

)

Array
(
    [0] => Array
        (
            [0] => <部屋|へや>
        )

    [1] => Array
        (
            [0] => 部屋|へや
        )

)

Thanks.

Community
  • 1
  • 1
mokalovesoulmate
  • 271
  • 2
  • 6
  • 18