Home  >  Q&A  >  body text

I don't understand why this code doesn't turn the title into the correct link

I have the following strings in my database:

"Minecraft is a sandbox video game developed by Mojang Studios from Sweden. Its concept revolve around procedurally generated world made of cubic blocks.

Minecraft is the best-selling game in history, having over 238 millions of copies sold.

<h1>Gameplay</h1>

Minecraft is played from first-person perspective. Gameplay includes mining, crafting, combat, building and terraforming. Usually when a new single-player game is started, a player is placed into a procedurally generated world. The player can encounter various "mobs"...

I'm trying to extract the title value from a string and convert it to a link using the following code

$heading='';
    $match1=array();
    $matched=preg_match("/<h\d>/",$part,$match1,PREG_OFFSET_CAPTURE);
    if($matched)
    {
        $match2=array();

        preg_match("#</h\d>#",$part,$match2,PREG_OFFSET_CAPTURE);
        $heading= substr($part,$match1[0][1]+4,$match2[0][1]);
        $heading="<a href='$heading'>$heading</a>";
    }

I expected the "title" variable to be , but the result is

P粉715228019P粉715228019427 days ago548

reply all(1)I'll reply

  • P粉776412597

    P粉7764125972023-09-12 18:15:37

    Based on @theforthbird's comment - you can use a single regex to capture the string you want, although parsing HTML with regex in general can cause a world of pain, so while this may suit your current purposes, If you need to do more along these lines, you're better off parsing the HTML into the DOM and extracting what you need.

    $heading = '';
    preg_match('/<h(\d)>(.*?)<\/h>/', $string, $matches);
    
    // print_r($matches); # uncomment this to see everything that was 'matched' by this regex
    
    if (!empty($matches[2])) {
        $heading = "<a href='{$matches[2]}'></a>";
    }

    reply
    0
  • Cancelreply