Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP preg_replace 'e' modifier dilemma
    primarykey
    data
    text
    <p>I've been fighting with this for a few days now and it looks like I really have to ask someone else.</p> <p>For the purposes of this thread, here is the problem: I am trying to manipulate a string using PHP's preg_replace and the 'e' modifier to execute a function within it. I want to change any instance of <code>[pdf]somefilename[/pdf]</code> to <code>&lt;a href="somefilename.pdf"&gt;PDF&lt;/a&gt; size('somefilename.pdf');</code></p> <p>So, for example maybe <code>[pdf]somefilename[/pdf]</code> would output a link to the file followed by (137MB).</p> <p>For some reason, however, the (137MB) part is being put at the <em>beginning</em> of the entire string, instead of where it's supposed to be. It seems like <code>preg_replace</code> is evaluating everything, then going back and executing the function and just throwing the result in the beginning of the string.</p> <p>So if I had <code>Some text blah, blah. Download the document: [pdf]somefilename[/pdf]</code> the regular expression outputs <code>(137MB) Some text blah, blah. Download the document: [link to document]</code> when it <em>should</em> output <code>Some text blah, blah. Download the document [link to document] (137MB)</code>.</p> <p>I'm lost. Google can't save me. Someone please help!</p> <p>Thanks in advance, Jeff</p> <p>Here's the code:</p> <pre><code>$bbcode_pdf_tag = '/\[pdf\](.*?)\[\/pdf\]/ie'; $html_pdf_tag = '"&lt;a href=\"$1.pdf\"&gt;PDF&lt;/a&gt;" .size("\\1.pdf")'; $decoded_data = preg_replace($bbcode_pdf_tag, $html_pdf_tag, $data); </code></pre> <p>EDIT: It's worth mentioning that size() is defined in a completely different file and this doesn't seem to happen when I use a built-in function such as strtolower(). Defining the size() function internally and using preg_replace_callback does not solve the problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload