最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - How to use a variable as a className inside preg_match_all parameters?

programmeradmin2浏览0评论

I am using regular expression to get the text inside a single p tag targeting its class. the problem is I want to use a php variable as class name and use a loop to change the class name each time.

$sample = get_the_content(); 


<button class="accordion">
preg_match('/<strong class="1">(.*?)<\/strong>/s', $sample, $match); 
echo $match[1]; 


$className = "1";

I want to use the $classname varialbe up there and increment it in a loop to get different paragraph content each time.

I hope I am clear in my question.

I am using regular expression to get the text inside a single p tag targeting its class. the problem is I want to use a php variable as class name and use a loop to change the class name each time.

$sample = get_the_content(); 


<button class="accordion">
preg_match('/<strong class="1">(.*?)<\/strong>/s', $sample, $match); 
echo $match[1]; 


$className = "1";

I want to use the $classname varialbe up there and increment it in a loop to get different paragraph content each time.

I hope I am clear in my question.

Share Improve this question edited Jan 11, 2020 at 11:59 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Jan 11, 2020 at 11:23 AshurAshur 233 silver badges8 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

Note: this question is not related to WordPress, but rather to PHP directly and should be asked at StackOverflow but I have provided a very basic answer below to help you on your way. However be aware that this answer is not necessarily the best way to go about solving your problem, nor is it meant to be efficient.

// assuming get_the_content() equates to ↓

$content = '<p class="1">P1 Content</p><p class="2">P2 Content</p><p class="3">P3 Content</p>';

$classNames = ['1', '2', '3'];

$matches = [];

foreach( $classNames as $className ) {

    $pattern = sprintf('\<p\sclass=\"%s\"\>(.*?)\<\/p\>', $className);

    preg_match("/{$pattern}/i", $content, $found); 

    $matches[] = $found;

}

var_dump($matches);

/*
array(3) {
  [0]=>
  array(2) {
    [0]=>
    string(27) "<p class="1">P1 Content</p>"
    [1]=>
    string(10) "P1 Content"
  }
  [1]=>
  array(2) {
    [0]=>
    string(27) "<p class="2">P2 Content</p>"
    [1]=>
    string(10) "P2 Content"
  }
  [2]=>
  array(2) {
    [0]=>
    string(27) "<p class="3">P3 Content</p>"
    [1]=>
    string(10) "P3 Content"
  }
}
*/

UPDATE 1:

If you do not have a fixed, known-list (ahead of time) of class names, then use the following example which utilises preg_match_all and looks for any <P> tag with class = NUMBER.

<p class="1">Content</p>
<p class="2">Content</p>
<!-- etc.. -->
<p class="234">Content</p>
$content = '<p class="1">P1 Content</p><p class="2">P2 Content</p><p class="3">P3 Content</p>';

$pattern = sprintf('\<p\sclass=\"%s\"\>(.*?)\<\/p\>', '[0-9]+');

var_dump($pattern);

preg_match_all("/{$pattern}/i", $content, $found);

var_dump($found);

/*
array(2) {
  [0]=>
  array(3) {
    [0]=>
    string(27) "<p class="1">P1 Content</p>"
    [1]=>
    string(27) "<p class="2">P2 Content</p>"
    [2]=>
    string(27) "<p class="3">P3 Content</p>"
  }
  [1]=>
  array(3) {
    [0]=>
    string(10) "P1 Content"
    [1]=>
    string(10) "P2 Content"
    [2]=>
    string(10) "P3 Content"
  }
}
*/
/**
 * Iterate over the results (if any) and do something with the content.
 */
if ( is_array($found) && isset($found[1]) ) {

    foreach( $found[1] as $content ) {

        echo $content;

    }

}

Results in:

P1 Content
P2 Content
P3 Content

I advise that you prefix your class names such as:

<p class="myprefix-1">Content</p>
<p class="myprefix-2">Content</p>
<!-- etc.. -->
<p class="myprefix-234">Content</p>

If so, make sure to update your regex pattern:

$pattern = sprintf('\<p\sclass=\"myprefix-%s\"\>(.*?)\<\/p\>', '[0-9]+');

REPL.IT DEMO

I found a solution for my question and I like to share it:

for this I simply created a php variable and added that in the middle of the parameter:

$sample = get_the_content(); 

$classes = '1';
<button class="accordion">
preg_match('/<p class="'.$classes.'">(.*?)<\/p>/s', $sample, $match); 
echo $match[1];

I am still confused how to increment the class by 1 to get different content each time.

Dear "Adam" this is what I am trying to do but I have issue in the length of array.

$tkey = 'post-update';

$update = new WP_Query(array(
    'posts_per_page' => -1,
    'tag' => $tkey
));

wp_reset_postdata();

if ($update->have_posts()) {
    while ($update->have_posts()) {
        $update->the_post();
        $sample = get_the_content();
    }
}

$classes = array();
array_push($classes, $match);

foreach ($classes as $names => $text) {

?>
    <button class="accordion">
        <?php
        preg_match('/<strong class="' . $text . '">(.*?)<\/strong>/s', $sample, $match);
        echo $match[1];
        ?>
    </button>
    <div class="panel">
        <p>
            <?php
            preg_match('/<p class="' . $text . '">(.*?)<\/p>/s', $sample, $match);
            echo $match[1];
            ?>
        </p>
    </div>
<?php } ?>
</div>
发布评论

评论列表(0)

  1. 暂无评论