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>"; }
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.