473,219 Members | 1,599 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,219 software developers and data experts.

The preg_replace limit and arrays.

Hi, :)

The preg_replace function...

preg_replace(pattern, replacement, subject [, int limit])

How on earth do you get the limit value to work with arrays?

In my code both the pattern and replacement parameters are arrays and I
only want a specified number of replacements to occur.

Can anyone help me find a solution? There must be one out there
somewhere. :-/

Thanks.

Marc :O)
Jul 17 '05 #1
2 3689
Afkamm wrote:
preg_replace(pattern, replacement, subject [, int limit])

How on earth do you get the limit value to work with arrays?
When you specify a limit, only that number of replacements
occur. With a limit of 1, preg_replace() tries to replace
each array element in pattern by the corresponding* element
in replacement once; a limit of 2, each element in pattern
by the corresponding one in replacement *twice*; and so on.
* Corresponding meaning the same position, not necessarily
the same index.
In my code both the pattern and replacement parameters are arrays and I
only want a specified number of replacements to occur.


Why would you pass to preg_replace() more patterns than you
want to search for and more replacements than you want to
make? If the number of elements in your arrays exceeds your
specified number, more replacements will occur than you
want. Shorten the arrays.

http://www.php.net/manual/en/function.array-slice.php

--
Jock
Jul 17 '05 #2
Drinkers log, beer date Sat, 05 Mar 2005 16:43:09 GMT. After downing
several pints in comp.lang.php, John Dunlop slurred the following.
Afkamm wrote:
preg_replace(pattern, replacement, subject [, int limit])

How on earth do you get the limit value to work with arrays?


When you specify a limit, only that number of replacements
occur. With a limit of 1, preg_replace() tries to replace
each array element in pattern by the corresponding* element
in replacement once; a limit of 2, each element in pattern
by the corresponding one in replacement *twice*; and so on.

I was under the impression that the limit value would stop the function
after that amount of replacements had been reached. But you're saying
that it only stops the array element it matched being replaced more
than what the limit is?

In my code both the pattern and replacement parameters are arrays
and I only want a specified number of replacements to occur.


Why would you pass to preg_replace() more patterns than you
want to search for and more replacements than you want to
make? If the number of elements in your arrays exceeds your
specified number, more replacements will occur than you
want. Shorten the arrays.

http://www.php.net/manual/en/function.array-slice.php


I'm trying to write a small mod/hack for the phpBB forum. What the
original code for smiley matching does, is create an array(pattern) of
all the smiley codes and at the same time creates a corresponding array
(replacement) with the html <img> tag code. It then uses the
preg_replace() function to go through the message post replacing any
codes found.

What I'm trying to do is limit the amount of times that smiley code
would be replaced by the corresponding html <img> code. ie. If 10
different smiley codes were used in a message and the limit was 5, then
only the first 5 would be replaced.

Is what I'm trying possible with a small piece of code or is it
something major? As anything large would slow the forum down some what.
:-/

Thanks for your reply. :)

Marc :O)
Jul 17 '05 #3

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

Similar topics

1
by: fartsniff | last post by:
i found this code out in the ng, and its seems long and clunky, i am still experimenting with preg_match and _replace, but the syntax is a bit confusing. it seems i always misplace or mistype...
6
by: JDJones | last post by:
Just want to verify. I have a form and I want to parse any semi-colons out of the submitted info and replace with commas. Would this be the correct way to do it? $question1 = preg_replace(";",...
3
by: TXSherry | last post by:
Hi, I cannot seem to wrap my brain around preg_replace. Though I've read the help file backwords and forwards. :/ Hoping someone can give me a solution here. Problem: Given string 'str'...
1
by: yawnmoth | last post by:
say i have the following script: <? $test = "aaaaa"; print '"' . preg_replace('/.*/','x',$test) . '"<br>'; $test = "\n\n\n\n\n"; print '"' . preg_replace('/.*/','x',$test) . '"'; ?> the...
7
by: Margaret MacDonald | last post by:
I've been going mad trying to figure out how to do this--it should be easy! Allow the user to enter '\_sometext\_', i.e., literal backslash, underscore, some text, literal backslash, underscore...
3
by: Charles | last post by:
I'm new to this regular expression stuff. I'd like to use preg_replace to eliminate a known multi-line signature from the body of an E-mail. Say the body text is in $body, and the sig is this ...
1
by: lawrence k | last post by:
Want to replace the limit clause in a query, but can't get it right. What's wrong with this: $pattern = "(.*)limit (.*)"; $replacement = '$1'; $replacement .= "LIMIT $limit"; $replacement .=...
7
by: monomaniac21 | last post by:
hi all using preg_replace how can i replace the letter i in a string with nothing (delete it) when it is the last letter or it is followed by an i? i have products that are listed in a db...
8
by: erikcw | last post by:
Hi all, I'm trying to write a regex pattern to use in preg_replace. Basically I want to put around every line (\n) in this variable. However, I need to exclude lines that already have brackets...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.