Jump to content

FAQ Script Help


hackerspk

Recommended Posts

i have a string for example

 $str = "[H]Test[/H] My test string [H]Test2[/H] My second Test string"; 

and i want to make a list inside "[H]" and make them click able like

<a href="#faq-1">Test</a>
<a href="#faq-2">Test2</a> 

and and also want to include this

<a name="faq-1"></a>Test
<a name="faq-2"></a>Test2 

in $str any help please

Link to comment
Share on other sites

iam unable to list all the words or sentenses inside th [H] [/H] tags

and then adding numbring to the links like

<a name="faq-1"></a>Test  = how to add 1

<a name="faq-2"></a>Test2  = how to add 2

my code is

$str = "this is [H]test[/H] this is 2nd [H]test[/H]";

$str = preg_replace("/\[H](.*?)\[/H]/is","<a name=\"faq-$i++\"></a>$1",$str);

echo $str;

Link to comment
Share on other sites

ok i got the soloution

 

<?php

$str = "[H]Test[/H] My test string [H]Test2[/H] My second Test string";

/**
* Helper class
*/
class FaqHelper {
    static $count = 1;
    static $listItems = array();
    static $prefix = 'faq-';

    static function GetList() {

        $items = '';
            foreach (self::$listItems as $id => $label) {
                $items .= '<li><a href="#' . self::$prefix . $id .'">' . $label . '</a></li>';
            }

        return '<ul>'. $items .'</ul>';
    }
}

// the callback function
function make_faq($matches)
{
    $id = FaqHelper::$count;
    $label = $matches[1];

    FaqHelper::$listItems[$id] = $label;

  $res = '<span id="'. FaqHelper::$prefix . $id .'">' . $label . '</span>';

    FaqHelper::$count++;

    return $res;
}

$text = preg_replace_callback(
    "#\[H\]([^\[]+)\[/H\]#",
    "make_faq",
    $str
);

$list = FaqHelper::GetList();

echo $list;
echo '<br /><br />';
echo $text;

?>

 

Link to comment
Share on other sites

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.