Asked  7 Months ago    Answers:  5   Viewed   38 times

Wanted to convert






You can do this with a regular expression:

preg_replace("/(<brs*/?>s*)+/", "<br/>", $input);

This if you pass in your source HTML, this will return a string with a single <br/> replacing every run of them.

Wednesday, March 31, 2021
answered 7 Months ago

Give this a try and see if it is what you want to extract from the tags.

preg_match_all('/( \w{1,}="\w{1,}"| \w{1,}=\w{1,}| \w{1,})/i', 
$result = $result[0];

The regex pulls each attribute, excludes the tag name, and puts the results in an array so you will be able to loop over the first and second attributes.

Saturday, May 29, 2021
answered 5 Months ago

You might use a preg_replace to do so:

Test 1:

$test = preg_replace('/(<.*”>)(.*)(</.*)/s', '<center>$2</center>', '<p style=“text-align:center; others-style:value;”>Content</p>');


Output 1:

It would return:

string(24) "<center>Content</center>"

RegEx 1:

The RegEx divides your inputs into three capturing groups, where the first and third groups can be assigned to open/close p tags.

enter image description here

RegEx 2:

You can further expand it, if you wish, with this RegEx for any other tags/quotations/contents that you may want. It would divide any tags with any quotations (" or ” or ' or ’) into five groups where the fourth group ($4) is your target content. This type of RegEx may be usually useful for single occurrence non-looping strings, since it uses (.*).

enter image description here

Test 2

$test = preg_replace('/<(.*)("|”|'|’)>(.*)(</.*)/s', '<center>$4</center>', '<p style=“text-align:center; others-style:value;”>Content</p>');


RegEx 3

If you may wish to get any specific attributes in style, this RegEx might help:


enter image description here

Test 3

$tags = [
    '0' => '<p style=“text-align:center; others-style:value;”>Content</p>',
    '1' => '<div style=‘text-align:left; others-style:value;’ class=‘any class’>Any Content That You Wish</div>',
    '2' => '<span style='text-align:right; others-style:value;' class='any class'>Any Content That You Wish</span>',
    '3' => '<h1 style=“text-align:justify; others-style:value;” class="any class">Any Content That You Wish</h1>',
    '4' => '<h2 style=“text-align:inherit; others-style:value;” class=“any class">Any Content That You Wish</h2>',
    '5' => '<h3 style=“text-align:none; others-style:value;” class=“any class">Any Content That You Wish</h3>',
    '6' => '<h4 style=“others-style:value;” class=“any class">Any Content That You Wish</h4>',


$RegEx = '/<(.*)(text-align:)(.*)(center|left|right|justify|inherit|none)(.*)("|”|'|’)>(.*)(</.*)/s';
foreach ($tags as $key => $tag) {
    preg_match_all($RegEx, $tag, $matches);
    foreach ($matches as $key1 => $match) {
        if (sizeof($match[0]) > 0) {
            $tags[$key] = preg_replace($RegEx, '<$4>$7</$4>', $tag);




Output 3

It would return:

array(7) {
  string(24) "<center>Content</center>"
  string(38) "<left>Any Content That You Wish</left>"
  string(40) "<right>Any Content That You Wish</right>"
  string(44) "<justify>Any Content That You Wish</justify>"
  string(44) "<inherit>Any Content That You Wish</inherit>"
  string(38) "<none>Any Content That You Wish</none>"
  string(86) "<h4 style=“others-style:value;” class=“any class">Any Content That You Wish</h4>"
Saturday, May 29, 2021
answered 5 Months ago

The variadiac php5.6+ version: (Offers the added benefits of not breaking on missing values and inserting null where values are missing.)

Code: (Demo)

var_export(array_map(function(){return implode(',',func_get_args());},...$text));

The non-variadic version:

Code: (Demo)

foreach($text as $i=>$v){


$text = [
    ['America','Japan','South Korea'],
    ['Washington DC','Tokyo','Seoul']

Output from either method:

array (
  0 => '001,America,Washington DC',
  1 => '002,Japan,Tokyo',
  2 => '003,South Korea,Seoul',

Nearly exact duplicate page: Combining array inside multidimensional array with same key

Saturday, May 29, 2021
answered 5 Months ago

Use implode:

 $tags = implode(', ', array('tag1','tag2','tag3','tag4'));
Tuesday, August 3, 2021
Yu Sasaki
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :