There is a code:

<?php echo $yml->replaceSpecial($this->renderPosition('image')); ?> 

There is a single line result:

 <image>site.com/image/img-1 site.com/image/img-2 site.com/image/img-3</image> 

How to break it by tags so that the final line takes the form:

 <image>site.com/image/img-1</image> <image>site.com/image/img-2</image> <image>site.com/image/img-3</image> 

Thank!

    3 answers 3

    Using regular expressions:

     $string = '<image>site.com/image/img-1 site.com/image/img-2 site.com/image/img-3</image>'; $pattern = ['~<(image)>([^<]+)</\1>~', '~(?<=\b|\s)(\S+)(?=\s|\b)~']; $replace = ['$2', '<image>$1</image><br />']; echo preg_replace($pattern, $replace, $string); 
    • @IZ if you don’t need a newline translation, then remove the <br /> tag from the second template <image>$1</image> <br /> - Edward
    • Super! Removed <br />. And it turned out that, and had to get! Edward - thank you so much! - IZ

    The most primitive approach for this string

     <image>site.com/image/img-1 site.com/image/img-2 site.com/image/img-3</image> 

    will use the function str_replace () with the replacement of the space on the line

     "</image>\n<image>" 
    • and it was possible to apply strip_tags .... and still explode is needed and the formation of new lines with image - Alexey Shimansky
    • @ Alexey Shimansky, the конечная строчкА приобретала вид: :; ) - Visman
    • I understood the idea - Alexey Shimansky
    • Thanks for the comments and participation! - IZ

    From the beginning, remove the image tags from the string using the str_replace method. Then we divide the string by spaces and get an array.

    This is the code:

     <?php $str1 = '<image>site.com/image/img-1 site.com/image/img-2 site.com/image/img-3</image>'; $str1 = str_replace("<image>", "", $str1); $str1 = str_replace("</image>", "", $str1); $arr = explode(' ', $str1); $finish_str = array(); for($i=0; $i<count($arr);$i++){ $arr[$i] = '<image>'.$arr[$i].'</image>'; } echo '<pre>'; print_r($arr); echo '</pre>'; ?> 

    And this is what we get:

     Array ( [0] => site.com/image/img-1 [1] => site.com/image/img-2 [2] => site.com/image/img-3 ) 
    • Razmik, thank you very much, it works. No offense, but for the decisions I choose the result of Edward. Anyone who will seek such a solution also advise using this method. Thank! - IZ