English 中文(简体)
Convert lib_string to <a href="string>string</a> 页: 1 Regex
原标题:Convert lib_string to <a href="string">string</a> w/o Regex
  • 时间:2011-11-15 20:31:37
  •  标签:
  • php

我需要将<条码>lib_someString 至<a href=“someString”>someString</a>,在使用地体[而不是regex]的案文栏内。

这里有一个例子说明我所说的确切含义:lib_12345<>/code> =><a href=“12345”>12345</a>。 我需要这样做,在案文的一栏中举几个例子。

以下是我的努力。 问题 我忘记的是,我的职能没有做任何事情(我刚刚获得平衡——回来)。

function extractLibId($val){ // function to get the "12345" in the above example 
    $lclRetVal = substr($val, 5, strlen($val));
    return $lclRetVal;
}

function Lib($text){ // does the replace for all lib_ instances in the text
    $lclVar = "lib_";
    $text = str_replace($lclVar, "<a href= ".extractLibId($lclVar)." >".extractLibId($lclVar)."</a>", $text);
    return $text;
}
最佳回答

登革角越快越清楚,你就没有必要要求你履行一切可能的平衡职能:

function Lib($text) {
    $count = null;
    return preg_replace( /lib_([0-9]+)/ ,  <a href="$1">$1</a> , $text, -1, $count);
}
$text =  some text lib_123123 goes here lib_111 ;
$text = Lib($text);

如果没有香料,则每当Lib2被称作某个地方时,就会失去切割包裹:

function extractLibId($val) {
    $lclRetVal = substr($val, 4);
    return $lclRetVal;
}
function Lib2($text) {
    $count = null;
    while (($pos = strpos($text,  lib_ )) !== false) {
        $end = $pos;
        while (!in_array($text[$end], array(   ,  , ,  . )) && $end < strlen($text)) 
            $end++;
        $sub = substr($text, $pos, $end - $pos);
        $text = str_replace($sub,  <a href=" .extractLibId($sub). "> .extractLibId($sub). </a> , $text);
    }
    return $text;
}

$text =  some text lib_123123 goes here lib_111 ;
$text = Lib2($text);

使用前线。

问题回答

Although it is possible to do what you need without regular expressions, you say you don t want to use them because of performance reasons. I doubt the other solution will be faster, so here is a simple regex to benchmark against:

echo preg_replace("/lib_(w+)/",  <a href="$1">$1</a> , $str);

As shown here: http://codepad.org/xGj78r9r

忽视如何优化这一荒谬的领域,即使是最简单的实施,而且最低的验证也只需要33%的时间才能达到顶峰。

<?php
function uselessFunction( $val ) {

    if( strpos( $val, "lib_" ) !== 0 ) {
    return $val;
    }

$str = substr( $val, 4 );

return "<a href="{$str}">{$str}</a>";

}


$l = 100000;

$now = microtime(TRUE);

while( $l-- ) {
preg_replace(  /^lib_(.*)$/ , "<a href="$1">$1</a>",  lib_someString  );
}

echo (microtime(TRUE)-$now)."
";
//0.191093

$l = 100000;
$now = microtime(TRUE);

while( $l-- ) {
uselessFunction( "lib_someString" );
}


echo (microtime(TRUE)-$now);
//0.127598
?>

If you re restricted from using a regex, you re going to have difficult time searching for a string you describe as "someString", i.e. not precisely known in advance. If you know the string is exactly lib_12345, for example, then set $lclVar to that string. On the other hand, if you don t know the exact string in advance, you ll have to use a regex via preg_replace() or a similar function.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...