Jump to content

extract 2 vars from a string and loop for more occurence


Recommended Posts

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

<?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;
?>

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

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;
?>

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.