Asked  7 Months ago    Answers:  5   Viewed   38 times

Wanted to convert

<br/>
<br/>
<br/>
<br/>
<br/>

into

<br/>

 Answers

91

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
 
kinske
answered 7 Months ago
14

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', 
    $content, 
    $result, 
    PREG_PATTERN_ORDER);
$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
 
kat_indo
answered 5 Months ago
58

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>');

var_dump($test);

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>');

var_dump($test);

RegEx 3

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

<(.*)(text-align:)(.*)(center|left|right|justify|inherit|none)(.*)("|”|'|’)>(.*)(</.*)

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>',
];

var_dump($tag);

$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);
            break;
        }

    }

}

var_dump($tags);

Output 3

It would return:

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

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){
    $result[]=implode(',',array_column($text,$i));
}
var_export($result);

Input:

$text = [
    ['001','002','003'],
    ['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
 
hnkk
answered 5 Months ago
93

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 :