简体   繁体   中英

preg_replace limit issue, handling array values

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文简体   中文繁体   中英对照 版本,有任何建议请联系yoyou2525@163.com。

I've been working with the Sphider search engine for an internal website, we need to be able to quickly search for contact details in exported .htm(l) files.

$fulltxt = ereg_replace("[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,3})", "<a href=\"mailto:\\0\">\\0</a>", $fulltxt);

I am replacing e-mail addresses with a convenient mailto: link so users can open Outlook straight from the search results.

However,

while (preg_match("/[^\>](".$change.")[^\<]/i", " ".$fulltxt." ", $regs)) {
    $fulltxt = preg_replace("/".$regs[1]."/i", "<b>".$regs[1]."</b>", $fulltxt);
    }

It replaces all matches in the search results with bold tags, which resuts into the tags been included in Outlook's 'To...' field. It looks something like this in HTML (thanks Yuriy ):

<a href="mailto:<b>name</b>.surname@domain"><b>name</b>.surname@domain</a>

I have tried adding a value to the 'limit' parameter:

while (preg_match("/[^\>](".$change.")[^\<]/i", " ".$fulltxt." ", $regs)) {
    $fulltxt = preg_replace("/".$regs[1]."/i", "<b>".$regs[1]."</b>", $fulltxt, 1);
    }

Supposingly this should be the solution to my problem by simply replacing only the first occurrence (being the name as the pattern is name-phone num-email and we always search by name ), instead it only makes it incredibly slow to the point i get a timeout message from the server. I've been trying various solutions but have been out of luck.

Any ideas? Am i doing something wrong?

Thanks.

(*Original heavily edited).

1 anwsers

Did I understand you right that something like this happens?

<a href="mailto:<b>email@domain</b>"><b>email@domain</b></a>

Why don't you put tags into search results first, and only then apply "mailto:" anchors to emails? Added 's would be easy to filter out in the patter on that second step.

1 preg_replace question

im trying to accomplish the following: search for a lowercase letter followed by uppercase letter. replace this with the lowercase letter, followed b ...

2 preg_replace issue

I would like to put a text instead of the string VERSION=20101203, my problem is only the first field of preg_replace, I am new to regular expresions. ...

3 preg_replace question

Hello I'm trying to do the following: Say I have an input string like this How can I convert it to the following format by using PHP I'm playin ...

4 preg_replace with arrays

My database has a table with 1000 terms and their definitions. I want to print those definitions and add a span tag to every word that is already a te ...

7 preg_replace problem

I'd like to match and extract variables from: {{variable:int}} variable would be anything a-z : is a separator int would be an integer 0-9 ...

9 preg_replace problems

I have a string The Incredible Hulk (2008) and use pattern to remove (2008). PHP code looks like this: And the result is: What am I doing wr ...

10 issue with preg_replace

This is a simple one :) I have this line which works great: What is the proper regex to remove the html entity version? This is the entities: ...

2010-08-11 20:34:33 2 53   php
暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM