-3

How to replace {asd} with 123 with php? I've a function to replace the string

   <?php
    function show($text) {
      $find = array(
       '{asd}' => "123"
      );
      return preg_replace($find,'',$text);
    }
    $text = "{asd}";
    $htmltext = show($text);
    echo $htmltext;
Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41
I.Nikolaew
  • 48
  • 9
  • why not use `str_replace` or `preg_repalce` directly ? and expected output is `{123}` or `123` ? – Jigar Jul 26 '15 at 09:30

2 Answers2

1

The solution is specific to OP question. There are much better solution available.

If you want output as 123:

function show($text) {
    $find = array(
    '/{asd}/',
    );
    return preg_replace($find,'123',$text);
}
$text = "{asd}";
$htmltext = show($text);
echo $htmltext;

If you want output as {123}:

function show($text) {
    $find = array(
    '/asd/',
    );
    return preg_replace($find,'123',$text);
}
$text = "{asd}";
$htmltext = show($text);
echo $htmltext;

Check example #2 in preg_replace for how to use array as parameters.

Example #2 Using indexed arrays with preg_replace()

<?php
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns = array();
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements = array();
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);
?>

The above example will output:

The bear black slow jumped over the lazy dog.

By ksorting patterns and replacements, we should get what we wanted.

<?php
ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);
?>

The above example will output:

The slow black bear jumped over the lazy dog.
Jigar
  • 3,256
  • 1
  • 30
  • 51
  • nope.... want by writing in html code {asd} to show 123 .. or {hhfdhdf} .. = 864 .. – I.Nikolaew Jul 26 '15 at 09:45
  • '{asd}' => '123' and it was so .. nope $patterns = array(); $patterns[0] = '/quick/'; $replacements = array(); $replacements[2] = 'bear'; – I.Nikolaew Jul 26 '15 at 09:46
  • how `{hhfdhdf} .. = 864` ? – Jigar Jul 26 '15 at 09:49
  • @I.Nikolaew please be specific in your question. comment section will only help me, not others. :) Please write the logic as well. Currently by looking at your question one can think only of replace from `{abc}` to `123` – Jigar Jul 26 '15 at 09:51
  • $find = array( '{asd}' => "123", '{hhfdhdf}' => "864", '{hijhi}' => "hfcfdh" ); – I.Nikolaew Jul 26 '15 at 09:52
  • sorry not able to get it. – Jigar Jul 26 '15 at 09:55
  • in phpbb the translation was done so ... – I.Nikolaew Jul 26 '15 at 09:58
  • how to do so if in html file there is {name} .. to show WebSite PHP Code: $lang = array( 'name' => 'WebSite', 'more' => 'More...' ); include('inc/message.html'); message.html code: Welcome in {name}. {more} – I.Nikolaew Jul 26 '15 at 11:13
0

I am not quite sure what your end result should be but for a string replace like that I would personally use sprintf.

http://php.net/manual/en/function.sprintf.php

Edit: You could simply do this.

$find['{asd}'] = "123";
$text = 'There are %d in here';
$output = sprintf($text, $find['{asd}']);
echo $output;

This would echo "There are 123 in here". You can use %s for string if needed (%d means integer).

Sinnbeck
  • 617
  • 4
  • 20
  • 56