473,323 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

back referencing to get an array index?

Hi,

My website needs to allow users to specify where a thumbnail image will
appear in the text ($articleText) they submit.
The thumbnail filenames are held in a single-dimensional array called
$photos
e.g. the users are to type [thumb4] to specify they want thumbnail four (
from array position 4 )

I was hoping the following expression would work, but it doesn't. The
returned text always includes <img src="thumbs/"> i.e. no image filename is
inserted. (I was expecting something like <img src="thumbs/dave.jpg"> )

$articleText = preg_replace("/\[thumb(\d+)\]/", "<img
src=\"thumbs/".$photos[$1]."\">", $articleText);

Can anyone help please?

Thanks,

--
Sorby
Jul 17 '05 #1
5 2504
Sorby wrote:
I was hoping the following expression would work, but it doesn't. The
returned text always includes <img src="thumbs/"> i.e. no image filename is
inserted. (I was expecting something like <img src="thumbs/dave.jpg"> )

$articleText = preg_replace("/\[thumb(\d+)\]/", "<img
src=\"thumbs/".$photos[$1]."\">", $articleText);


The expression "<img src=\"thumbs/".$photos[$1]."\">" is evaluated to
"<img src=\"thumbs/\">" and then passed as a parameter to preg_replace().

Since you need to dereference a variable at _replacement_ time you need
to execute code; either use preg_replace_callback to call a function, or
use the /e modifier on the regular expression to make the replacement
text a PHP expression which is evaluated.

For instance:
$articleText = preg_replace("/\[thumb(\d+)\]/e",
"'<img src=\"thumbs/'.\$photos[$1].'\">'", $articleText);

I generally prefer to use preg_replace_callback and make a function, as
the extra escaping rules for writing code inside a string make the code
harder to read and easier to make mistakes in, though more for more
complicated code than this.

$articleText = preg_replace_callback("/\[thumb(\d+)\]/",
'thumbReplace', $articleText);
# ...
function thumbReplace($matches) {
global $photos;
return "<img src=\"thumbs/" . $photos[$matches[1]] . "\">";
}

-- brion vibber (brion @ pobox.com)
Jul 17 '05 #2
"Brion Vibber" <br***@pobox.com> wrote in message
news:39*************@individual.net...
Sorby wrote:
I was hoping the following expression would work, but it doesn't. The
returned text always includes <img src="thumbs/"> i.e. no image filename
is inserted. (I was expecting something like <img
src="thumbs/dave.jpg"> )

$articleText = preg_replace("/\[thumb(\d+)\]/", "<img
src=\"thumbs/".$photos[$1]."\">", $articleText);


The expression "<img src=\"thumbs/".$photos[$1]."\">" is evaluated to
"<img src=\"thumbs/\">" and then passed as a parameter to preg_replace().

Since you need to dereference a variable at _replacement_ time you need to
execute code; either use preg_replace_callback to call a function, or use
the /e modifier on the regular expression to make the replacement text a
PHP expression which is evaluated.

For instance:
$articleText = preg_replace("/\[thumb(\d+)\]/e",
"'<img src=\"thumbs/'.\$photos[$1].'\">'", $articleText);

I generally prefer to use preg_replace_callback and make a function, as
the extra escaping rules for writing code inside a string make the code
harder to read and easier to make mistakes in, though more for more
complicated code than this.

$articleText = preg_replace_callback("/\[thumb(\d+)\]/",
'thumbReplace', $articleText);
# ...
function thumbReplace($matches) {
global $photos;
return "<img src=\"thumbs/" . $photos[$matches[1]] . "\">";
}


Thank you *very* much Brion - that works great! [1]

[1] I also had to replace my original (\d+) with ([[:digit:]]+)

--
Sorby
Jul 17 '05 #3
.oO(Sorby)
[1] I also had to replace my original (\d+) with ([[:digit:]]+)


That shouldn't be necessary ...

Micha
Jul 17 '05 #4
"Michael Fesser" <ne*****@gmx.net> wrote in message
news:ck********************************@4ax.com...
.oO(Sorby)
[1] I also had to replace my original (\d+) with ([[:digit:]]+)


That shouldn't be necessary ...


I believe you - but in my case it didn't work until I changed it!
Maybe my syntax is wrong? I'm pretty new to regular expressions.

--
Sorby
Jul 17 '05 #5
Sorby wrote:
[1] I also had to replace my original (\d+) with ([[:digit:]]+)

Shouldn't have to; they are equivalent.

[ ... ]
Maybe my syntax is wrong?


No, it looks fine to me, even though it's not documented in
the Manual's description of pattern syntax.

Not having read the PCRE man pages before, I didn't know you
could use POSIX character classes in PCREs. The Manual has
lifted large chunks from the man pages, but has left behind
the entire section on POSIX character classes.

http://www.php.net/manual/en/ref.pcre.php

http://www.pcre.org/pcre.txt

--
Jock
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
2
by: xcomm | last post by:
Hi All, <?php $vars= array("_SERVER","_SERVER","_SERVER","_SERVER","_SERVER","_SERVER"); foreach($vars as $var) { if(isset($$var))echo("$var: ${$var}<br>\n"); } ?> php.net:
7
by: Asfand Yar Qazi | last post by:
Hi, I've got this class, you see: class Vec4 { Vec4(); ... float data; float& x;
5
by: hibernate | last post by:
I'm somewhat new to javascript/DHTML, and this problem has been plaguing me. I have made an 'array' of <div> tags within my html document like so: <div id="menu"> menu1 </div> <div id="menu">...
11
by: Edd | last post by:
Hello all, I've made a data structure and an associated set of functions to enable me to store a dynamically-sized array of elements of whatever data type I like. Well that's the idea anyway......
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
14
by: David Veeneman | last post by:
I have a two-dimensional array with 6 x 7 elements. I'd like to be able to reference the elements sequentially, as if the rows were laid end-to-end. I know the Array.Length property will give me...
0
by: Buglish | last post by:
Hi, Task : -Capture a HTML table with use of regular expression from a text string buffer(entire document). –Pass it to another function to create a multi dimension array out of it. - Pass it...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.