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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_replace syntax problem

Status
Not open for further replies.

bodington

Technical User
Aug 26, 2002
12
US
I am converting xml to pdf via php. I am using fpdf.

I need to replace the string "PageBreak" with the code in fpdf to add a new page .. ie. AddPage().

So, I used this:
Code:
    $strText = preg_replace("/PageBreak/",$this->AddPage(),$strText);

But instead of replacing the string "PageBreak" with the code AddPage(), it adds the code AddPage() before each string - including the ones that do not have PageBreak anywhere in them.

In comparison, this code works perfectly fine to replace this string "Page##of####" with with the appropriate fpdf code for declaring the page number and number of pages so it prints out like "Page 1 of 50"...

Code:
$strText = preg_replace("/([^#]|^)##([^#]|$)/", "\\01" . $this->PageNo() . "\\02", $strText);
$strText = preg_replace("/([^#]|^)####([^#]|$)/", "\\01" . "{nb}" . "\\02", $strText);

I have no idea why this isn't working and I'm super frustrated. I could really use some help!

Anyone know what I'm doing wrong? Thanks in advance for any/all help!
 
as thenewa2x implies, i don't think a substitution will work. the replace text is a method of the current object that creates programmatically a new page. what preg_replace does is to take the found text and replace it with the new text. the current method does not do this.

what you need to do is to split the string holding the doc contents by the string PAGEBREAK and then reassemble as different pages.

Code:
$docContents = $strText;
$pages = explode ('PageBreak', $docContents);
//then
foreach($pages as $page){
  //create a page
  $this->addPage();
  //put the page in
  $this->write (5, $page);
}
 
Thank you both for your suggestions. I tried them both and neither worked as I needed it to - but got me to thinking. So, I ended up just writing an if statement and it works fine now (can't believe I didn't think of it in the first place!). Here's my solution:

Code:
If ($strText == "PageBreak")
{
$strText = preg_replace("/PageBreak/",$this->AddPage(),$strText);
}

Again, thanks so much for your help! Your thoughs nudged me in the right direction.
 
If [tt]$strText[/tt] will equal [tt]PageBreak[/tt], could you just do this instead?

Code:
if ( $strText == "PageBreak" )
{
    $strText = $this->AddPage();
}

 
personally, i think it is "luck" that the OP's method works at all. it is a misuse of preg_replace. the "luck" is that the method returns NULL rather than some other content and that the method has been coded to attach to a property of the class.

following thenewa2x's variant, my take on this would be

Code:
if ( $strText == "PageBreak" ){
    $strText = '';
    $this->AddPage();
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top