Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with preg_replace_callback

Status
Not open for further replies.

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
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.
 
this should work

Code:
<?php
$pattern = '/{myCap}(.?){\/myCap}(.*?)(<\/div>|<\/p>|<br\W*>)/is';
$replace = '<div class="capbox"><span class="dropit">$1</span>$2</div>$3';
$row->text = preg_replace( $replaceregex, $replace, $row->text );
?>

although it is far from perfect. for example a string like this

Code:
<div class="wrapper">
{myCap}H</myCap}ello World
<div class="internal">This is a short paragraph</div>
<div class="internal">This is another sub-para</div>
</div> <!--- close the wrapper div --->

will cause the capbox div to wrap only the first two lines.

note also that the replacement text should use a capbox class rather than an id as there is a possibility of multiple replacements.
 
Hi, and thanks for the tip. Unfortunately, it doesn't seem to work.

I have 3 paragraphs, the first two of which have the tags around the first letter.
After running the replace above, I get the following:
Code:
<p align="justify"><div class="capbox"><span class="dropit">{dropit}W{/dropit}</span></p></div>
So it is applying the div and span, but doesn't remove the original tags.
Also, that is all that is left of my original 3 paragraphs - the rest is gone.
 
well, it's not looking for {dropit} but for {myCap}.

if you provide a few paragraphs of input and the desired output, that would help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top