2015-08-14 3 views
-2

У меня проблема с разбором текста в квадратных скобках с помощью PHPPHP Parse html start and end bracket

Вот что я хочу сделать. У меня есть textarea, и я ввожу эту информацию.

Lorem Ipsum is simply dummy text of the printing and typesetting industry 

[if city="x"]My specific agreement text for the city x[/if] 

Lorem Ipsum is simply dummy text of the printing and typesetting industry.Lorem Ipsum is simply dummy text of the printing and typesetting industry 

[if city="y"]My specific agreement text for the city y[/if] 

Lorem Ipsum is simply dummy text of the printing and typesetting industry 

[if city="y"]My specific agreement text 2 for the city y[/if] 

Тогда я хотел бы сделать текст следующим образом.

<?php 
$textarea = $_POST['textarea']; 
$city = "y"; 

// DO SOME MAGIC I DON'T KNOW... To remove the text between the [if city="x"]Text[/if] and keep the textsnippets between [if city="y"]Text[/if] 

// END OF MAGIC. 

echo $textarea // Now the text that are in brackets of city = y only shows. 
?> 

Конечный результат должен быть таким (если эхо)

Lorem Ipsum просто манекен текста печати и верстки промышленности

Lorem Ipsum просто манекен текста печати и наборщик промышленность. Lorem Ipsum просто манекен текста печати и верстки промышленности

Мой конкретный текст соглашения для города у

Lorem Ipsum просто манекен текста печати и верстки промышленности

Мои конкретный текст соглашения 2 для города y

+0

К сожалению, что вы хотите сделать? Совместить весь текст между ']' и '[' в четных пронумерованных строках? Или на самом деле оценить выражение в исходном '[...'] 'на этих строках? – Richard

+1

Вы что-то пробовали? предоставьте код – rsz

+0

. Что вы хотите достичь здесь? Непонятно ... не могли бы вы рассказать? – jimmypage

ответ

0

Так что благодаря регулярному выражению "анонимный" я мог бы найти решение, которое работает для меня. Не думайте, что это приманка, но она работает.

$city = 'x'; 
$string = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry 
<br><br> 
[if city="x"]My specific agreement text for the city x[/if] 
<br><br> 
Lorem Ipsum is simply dummy text of the printing and typesetting industry.Lorem Ipsum is simply dummy text of the printing and typesetting industry 
<br><br> 
[if city="y"]My specific agreement text for the city y[/if] 
<br><br> 
Lorem Ipsum is simply dummy text of the printing and typesetting industry 
<br><br> 
[if city="y"]My specific agreement text 2 for the city y[/if]'; 


$reg = preg_match_all('(\[if.*](.*)\[\/if\])', $string, $matches); 


foreach($matches[0] as $key => $value) { 
    if(strpos($value,'city="'. $city .'"') !== false) { 

    } else { 
     $string = str_replace($value, '', $string); 
    } 
} 

$string = str_replace('[if city="'. $city .'"]', '', $string); 
$string = str_replace('[/if]', '', $string); 


echo $string;