hackerspk Posted August 14, 2011 Share Posted August 14, 2011 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 https://forums.phpfreaks.com/topic/244736-faq-script-help/ Share on other sites More sharing options...
trq Posted August 14, 2011 Share Posted August 14, 2011 Where exactly are you stuck? Link to comment https://forums.phpfreaks.com/topic/244736-faq-script-help/#findComment-1257046 Share on other sites More sharing options...
hackerspk Posted August 14, 2011 Author Share Posted August 14, 2011 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 https://forums.phpfreaks.com/topic/244736-faq-script-help/#findComment-1257049 Share on other sites More sharing options...
hackerspk Posted August 14, 2011 Author Share Posted August 14, 2011 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 https://forums.phpfreaks.com/topic/244736-faq-script-help/#findComment-1257052 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.