Em Andamento

Simple PHP regex (search and replace) function

I need a PHP function which reads a string. This string contains some keywords which are between %, for example %KEYWORD%. This expression should be replace through the constant KEYWORD. Check before if this constant exists, when not, do not replace. Moreover the string contains links with following syntax: [FILENAME_XXX]Some Text[/FILENAME_XXX] In This case you have to create a link (FILENAME_XXX is placeholder for a constant with an url) with link text between this link tags [XXX] [/XXX]. Check before if this constant exists, when not, do not replace. The first part should solved first, then the second, because I want that dynamic part like [FILENAME_ACCOUNT]%ANY_CONSTANT%[/FILENAME_ACCOUNT] works as well. It should also works, when on tag appears several times, as in side the example (FILENAME_ACCOUNT tag is use several times)

The needed code should be in one function:

Exmaple:

// constants are defined somewhere

define('USER_NAME', 'Phil');

define('FILENAME_ACCOUNT', '[url removed, login to view]');

define('FILENAME_LOGOUT', '[url removed, login to view]');

function your_function($str)

{

// your work is inside this function onaly

return $new_string;

}

// example string

$str = "<strong>Hi, %USER_NAME%.</strong> Click [FILENAME_ACCOUNT]here[/FILENAME_ACCOUNT] for visiting your [FILENAME_ACCOUNT]Account[/FILENAME_ACCOUNT]. (You are not %USER_NAME%? Click [FILENAME_LOGOUT]here[/FILENAME_LOGOUT].)";

$new_str = your_function($str);

echo $new_str;

/*

Should Show:

<strong>Hi, Phil.</strong> Click <a href="[url removed, login to view]">here< / a > for visiting your <a href="[url removed, login to view]">Account< / a >. (You are not Phil? Click <a href="[url removed, login to view]">here< / a >.)

*/

please notice: "< / a >" should have no " ", rentacoder does not display it, when I write it correct.

Edit: I changed the exmaple sentence from german to english, that it is easer for you to understand. In my php script, I can define translations, and Inside these translations, I like to show constants as text and a possibility to show dynamic links.

Habilidades: Engenharia, MySQL, PHP, Arquitetura de software, Teste de Software, Hospedagem Web, Gestão de Site , Teste de Website

Ver mais: use case simple example, text string search, string search in c, some search string, search text string, search string in c, search string c, search string, search c string, regex is, regex in c, regex example, regex c, c string search, c search string, regex &, write a simple php script, str, replace, Regex, php regex, php german, php function , Phil, engineering dynamic

Acerca do Empregador:
( 20 comentários ) Germany

ID do Projeto: #3042843

Premiar a:

alekseyzapparov

See private message.

$7 USD em 1 dia
(10 Avaliações)
4.3

11 freelancers estão ofertando em média $10 para este trabalho

afifaumersl

See private message.

$8.5 USD em 1 dia
(15 Comentários)
5.8
phpdevteam

See private message.

$8.5 USD em 1 dia
(57 Comentários)
5.4
rogersilverstone

See private message.

$8.5 USD em 1 dia
(126 Comentários)
5.0
vw7109022vw

See private message.

$12.75 USD em 1 dia
(24 Comentários)
4.6
m3h3divw

See private message.

$8.5 USD em 1 dia
(27 Comentários)
4.5
getitrightvw

See private message.

$12.75 USD em 1 dia
(31 Comentários)
3.9
jack0stone

See private message.

$10.2 USD em 1 dia
(9 Comentários)
2.6
softtechrey

See private message.

$8.5 USD em 1 dia
(0 Comentários)
0.0
bsaid

See private message.

$12.75 USD em 1 dia
(0 Comentários)
0.0
yasirmalik

See private message.

$10.2 USD em 1 dia
(0 Comentários)
0.0