arst06d
Programmer
- Nov 29, 2002
- 324
Hi
I'm trying to create a bit of code to manipulate text in order to do a bit of fancy css coding and produce a dropped capital wherever a user requires.
eg user would input "{myCap}H{/myCap}ello World" and my function would strip out the {}tags and wrap the H in some CSS coding, then spit out the rest of the text.
That bit is no problem, but when the user wants more than one dropped cap in the page, I cannot seem to get it to work.
My regex is ({myCap}.{/myCap}).*(<br/>|<br />|</p>)
ie a single character between my tags then the rest of the text until the end of the paragraph denoted by the html line or paragraph breaks.
The bit that eludes me is how to insert a closing </div> before the last </br> or </p>, and then move onto the next paragraph.
so far I have
This replaces the tags OK and styles the first capital designated in the page, but the closing </div> is placed after all the text and no other {} tag set is affected.
I am completely new to PHP and regular expressions, and any advice you can give will be appreciated.
I'm trying to create a bit of code to manipulate text in order to do a bit of fancy css coding and produce a dropped capital wherever a user requires.
eg user would input "{myCap}H{/myCap}ello World" and my function would strip out the {}tags and wrap the H in some CSS coding, then spit out the rest of the text.
That bit is no problem, but when the user wants more than one dropped cap in the page, I cannot seem to get it to work.
My regex is ({myCap}.{/myCap}).*(<br/>|<br />|</p>)
ie a single character between my tags then the rest of the text until the end of the paragraph denoted by the html line or paragraph breaks.
The bit that eludes me is how to insert a closing </div> before the last </br> or </p>, and then move onto the next paragraph.
so far I have
Code:
$replaceregex = "#({myCap}.{/myCap}).*(<br/>|<br />|</p>)#s";
//assume $row contains the text to be searched
$row->text = preg_replace_callback( $replaceregex, 'myCap_replacer', $row->text );
function myCap_replacer( &$matches ) {
//get the character within the tags
$theCap = substr ( $matches[0], 7 , 1 );
$theRest = substr ( $matches[0], 16 );
return '<div id="capbox"><span class="dropit">'.$theCap.'</span>'.$theRest.'</div>';
}
This replaces the tags OK and styles the first capital designated in the page, but the closing </div> is placed after all the text and no other {} tag set is affected.
I am completely new to PHP and regular expressions, and any advice you can give will be appreciated.