drisate Posted October 25, 2010 Share Posted October 25, 2010 Hey guys i need help. I am a newbie in regex and i need to extract 2 vars from a string that contains the following.: [invisible] [titre]$var1[/titre] [texte]$var2[/texte] [/invisible] I need to point out that the string contains more [titre] and [texte] tags that are part of diffrent groups than [invisible]. And one more thing, the [invisible] tag can occure more that one time. thx in advance ;-) Link to comment https://forums.phpfreaks.com/topic/216804-extract-2-vars-from-a-string-and-loop-for-more-occurence/ Share on other sites More sharing options...
sasa Posted October 26, 2010 Share Posted October 26, 2010 <?php $test = ' [invisible] [titre]$var1[/titre] [texte]$var2[/texte] [/invisible]'; //first select invisible tag(s) preg_match_all('/\[invisible\](.*?)\[\/invisible\]/s',$test,$out); if(count($out[1]) > 0){ $out = implode('', $out[1]); // put all invisible tags together preg_match('/\[titre\](.*?)\[\/titre\]/s', $out, $out1); // extract var 1 $var1 = $out1[1]; preg_match('/\[texte\](.*?)\[\/texte\]/s', $out, $out1); $var2 = $out1[1]; } echo $var1, ' - ', $var2; ?> Link to comment https://forums.phpfreaks.com/topic/216804-extract-2-vars-from-a-string-and-loop-for-more-occurence/#findComment-1126557 Share on other sites More sharing options...
drisate Posted October 26, 2010 Author Share Posted October 26, 2010 thanks sasa thats almost what i need What i need to do in replace the [titre] part into a link like this: <a href="#" onclick="showhide('$id'); return(false);">Title sample 1</a> The $id could be a md5 version of the title + a random number between 1 and 99999 added in case theres more then one same title Then i need to replace the [texte] part into this: <div style="display: none;" id="$id"> Texte sample 1 </div> The $id needs to be the same for both the title and the texte. Then i take the new file content and replace the existing content with the new version using file_put_contents($file, $current); 1. Problem is there can be more then one replace to do in the $current string. 2. Once i did get the $var1 and $var2 extracted and changed how do i put it back in $content? I am pretty sure preg_match_all is not what i need but your the expert lol Here my code: <?php // $file = $dossier_cam_autres_fichiers.'/'.nomValide($_FILES['fichierhtml']['name']); // Open up the file and read the content // $current = file_get_contents($file); // SAMPLE CODE $current = '[invisible] [titre]Title sample 1[/titre] [texte]Texte sample 1[/texte] [/invisible] [invisible] [titre]Title sample 2[/titre] [texte]Texte sample 2[/texte] [/invisible] [invisible] [titre]Title sample 3[/titre] [texte]Texte sample 3[/texte] [/invisible]'; //first select invisible tag(s) preg_match_all('/\[invisible\](.*?)\[\/invisible\]/s',$current,$out); if(count($out[1]) > 0){ $out = implode('', $out[1]); // put all invisible tags together preg_match('/\[titre\](.*?)\[\/titre\]/s', $out, $out1); // extract var 1 $var1 = '<a href="#" onclick="showhide(\''.md5($out1[1]).'\'); return(false);">'.$out1[1].'</a>'; preg_match('/\[texte\](.*?)\[\/texte\]/s', $out, $out1); $var2 = '<div style="display: none;" id="'.md5($out1[1]).'">'.$out1[1].'</div>'; } // How the hell do i put back those vars into the $current >.< echo $current; // Put the result back into the file // file_put_contents($file, $current); ?> Link to comment https://forums.phpfreaks.com/topic/216804-extract-2-vars-from-a-string-and-loop-for-more-occurence/#findComment-1126649 Share on other sites More sharing options...
sasa Posted October 26, 2010 Share Posted October 26, 2010 if i understu what you want try <?php // $file = $dossier_cam_autres_fichiers.'/'.nomValide($_FILES['fichierhtml']['name']); // Open up the file and read the content // $current = file_get_contents($file); // SAMPLE CODE $current = '[invisible] [titre]Title sample 1[/titre] [texte]Texte sample 1[/texte] [/invisible] [invisible] [titre]Title sample 2[/titre] [texte]Texte sample 2[/texte] [/invisible] [invisible] [titre]Title sample 3[/titre] [texte]Texte sample 3[/texte] [/invisible]'; function my_replace($m){ static $i = 0; $id=md5($i++); $m[1] = preg_replace('/\[texte\](.*?)\[\/texte\]/s', '<a href="#" onclick="showhide(\''.$id.'\'); return(false);">\1</a>', $m[1]); $m[1] = preg_replace('/\[titre\](.*?)\[\/titre\]/s', '<div style="display: none;" id="'.$id.'"> \1 </div>', $m[1]); return $m[1]; } //first select invisible tag(s) $out = preg_replace_callback('/\s*\[invisible\](.*?)\[\/invisible\]/s', 'my_replace',$current); echo $out; ?> Link to comment https://forums.phpfreaks.com/topic/216804-extract-2-vars-from-a-string-and-loop-for-more-occurence/#findComment-1126733 Share on other sites More sharing options...
drisate Posted October 26, 2010 Author Share Posted October 26, 2010 wow thx bro it's working great now :-) It opens a html page replaces the BBCode and seves it <?php $file = $dossier_cam_autres_fichiers.'/'.nomValide($_FILES['fichierhtml']['name']); // Ouvre un fichier pour lire un contenu existant $current = file_get_contents($file); // on replace le BBCODE function my_replace($m){ static $i = 0; $id=md5($i++); $m[1] = preg_replace('/\[titre\](.*?)\[\/titre\]/s', '<a href="#" onclick="showhide(\''.$id.'\'); return(false);">\1</a>', $m[1]); $m[1] = preg_replace('/\[texte\](.*?)\[\/texte\]/s', '<div style="display: none; border-width:1px;border-style:solid;border-color:#000000;width:100%;" id="'.$id.'"><div style="margin:10px"> \1 </div><div align="right" style="margin:10px"><a href="#'.$id.'" onclick="showhide(\''.$id.'\'); return(false);">Fermer</a></div></div>', $m[1]); return $m[1]; } //first select invisible tag(s) $out = preg_replace_callback('/\s*\[invisible\](.*?)\[\/invisible\]/s', 'my_replace',$current); if ( !strstr($out, "function showhide(id){") ) { $out = '<script> function showhide(id){ if (document.getElementById){ obj = document.getElementById(id); if (obj.style.display == "none"){ obj.style.display = ""; } else { obj.style.display = "none"; } } } </script> '."\n\n".$out; } // Écrit le résultat dans le fichier file_put_contents($file, $out); ?> Link to comment https://forums.phpfreaks.com/topic/216804-extract-2-vars-from-a-string-and-loop-for-more-occurence/#findComment-1126767 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.