preg-replace-callback

Preg_replace or preg_replace_callback?

三世轮回 提交于 2019-12-13 15:04:48
问题 I have links on some pages that use an old system such as: <a href='/app/?query=stuff_is_here'>This is a link</a> They need to be converted to the new system which is like: <a href='/newapp/?q=stuff+is+here'>This is a link</a> I can use preg_replace t0 change some of what i need to, but i also need to replace underscores in the query with +'s instead. My current code is: //$content is the page html $content = preg_replace('#(href)="http://www.site.com/app/?query=([^:"]*)(?:")#','$1="http:/

PHP replace dynamic content based on position in string

自闭症网瘾萝莉.ら 提交于 2019-12-13 10:18:48
问题 What I would like : The values to replace are between {{ }} . Input : "this is letter {{ A }} and {{ B }}" but it can change : "this is letter {{ AAAA }} and {{ BBB }}" with array ("C", "D") Output : "this is letter C and D I don't know in advance the string between {{ }} and I would like to extract those strings and replace them with another value : What I tried : $body = "This is a body of {{awesome}} text {{blabla}} from a book."; //Only work if we know the keys (awesome, blabla,...) $text

preg_replace_callback with array pattern and replacement

梦想与她 提交于 2019-12-13 06:15:18
问题 I have a function which uses preg_replace() where the pattern and replacements are arrays. I need a counter to track the replacements, so I am converting the function to use preg_replace_callback with a closure, but I can't seem to find a way to distinguish which pattern the match being passed to the callback matches. Is there a way to do array => array replacement using preg_replace_callback? Ideally this is what I'd like to work, but obviously it won't since $pattern and $replace are

Replacing end div tags using preg_replace_callback function

随声附和 提交于 2019-12-13 00:24:37
问题 I am trying to develop a PHP script that replaces all divs in an HTML string with paragraphs except those which have attributes (e.g. <div id="1"> ). The first thing my script currently does is use a simple str_replace() to replace all occurrences of <div> with <p> , and this leaves behind any div tags with attributes and end div tags ( </div> ). However, replacing the </div> tags with </p> tags is a bit more problematic. So far, I have developed a preg_replace_callback function that is

Can't convert preg_replace() with modifier /e to preg_replace_callback()

♀尐吖头ヾ 提交于 2019-12-12 21:30:06
问题 I am using preg_replace() to replace {#page} with the actual value of the variable $page . Of course I have a lot of {#variable} , not just {#page} . For example: $uri = "module/page/{#page}"; $page = 3; //preg_replace that its working now $uri_to_call = $uri_rule = preg_replace('/\{\#([A-Za-z_]+)\}/e', "$$1", $uri); And I get the result "module/page/3"; After update to PHP 5.4 i get the error: Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead And I

Collapsable print_r() tree with PHP 7 (w/o preg_replace() and /e)

眉间皱痕 提交于 2019-12-12 16:49:02
问题 In order to print_r a collapsable tree, I'm currently using his code which uses preg_replace() and the /e modifier, which is depreciated in PHP7: <?php function print_r_tree($data) { // capture the output of print_r $out = print_r($data, true); // replace something like '[element] => <newline> (' with <a href="javascript:toggleDisplay('...');">...</a><div id="..." style="display: none;"> $out = preg_replace('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iUe',"'\\1<a href=\

trouble with preg_replace_callback screwing up text

拜拜、爱过 提交于 2019-12-12 04:32:31
问题 I am trying to replace bbcode quotes like this: [quote=username]text[/quote] which works fine. If someone is quoting someone, who is also quoting someone else, the problem arises that it only appears to replace one of them, like this sort of text: [quote=person1][quote=person2][quote]test quoted text[/quote] another quote[/quote] one more quote[/quote] Here are my functions: // replace specific-user quotes, called by quotes() function replace_quotes($matches) { global $db; $find_quoted = $db-

How to call a function within preg_replace()?

倖福魔咒の 提交于 2019-12-12 04:14:48
问题 I've written a function that replaces certain patterns in a blog. For example when someone types: :) this function replaces it with a smiley emoticon. However now I'm trying to do something special, but don't know how to accomplish it. I would like to parse a match to another function like so: $pattern[] = "/\[ourl\](.*?)\[\/ourl\]/i"; $replace[] = "" . getOpenGraph("$1") . ""; $value = preg_replace($pattern, $replace, $value); If someone uses [ourl]www.cnn.com[/ourl] this function will

Replace based on regex and return manipulated string

有些话、适合烂在心里 提交于 2019-12-12 02:48:39
问题 I have a logic query as a string, for example: mydate == 'one day ago' && mydate > 2014-05-16 16:00:00 I need every instance of a date replaced by a timestamp. My current code can replace the YYYY-MM-DD HH:MM:SS date-time to a timestamp using strtotime() : $my_string = "mydate == 'one day ago' && mydate > 2014-05-16 16:00:00"; // Pattern: *YYYY-MM-DD HH:MM:SS* $pattern = '((?:2|1)\\d{3}(?:-|\\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\\s)(?:(?:[0-1][0-9]

PHP: run function at certain places (preg_callback?)

二次信任 提交于 2019-12-12 01:39:34
问题 I'm trying to run a function whenever there's a [%xxx%] (acting as a placeholder, if you will), e.g.: Bla bla bla blabla. Blablabla bla bla. [%hooray%] Blabla hey bla bla. [%yay%] blabla bla. I'm pretty much a PHP beginner, but I managed to crack my head through and come up with the following (yay to me - I somehow managed to understand the basics of regular expressions!): $maintext = preg_replace("#\[%(.{1,20})%\]#", "display_products('$1')"), $maintext); echo $maintext; I tried working with