I know that there are many tutorials and there is a question about taking place in some strings.
But I say that I have such a string
$ string = "Hello! [Number: 0]"; And such an example table
$ array = array (); $ Array [0] = array ('name' = & gt; "no"); $ Array [1] = array ('name' = & gt; "nam"); Now what do I need to do that PHP should first look for patterns like [num: x] , where x is a valid key Replace with the array and then with the key of the array matching it. For example, the above string should be created: Hi! N I was thinking of doing this way:
- Search for patterns.
- If found, then call a function that checks if the number is valid or not.
- If valid, will return the name of that key as name 0 or 1 etc.
- Changes the returned value from the function in PHP, instead of the pattern string.
But I can not find any way to execute this idea. How do I match that method and call the function for every match?
This is the only way I am thinking of doing. Any other way will work too.
If you have any doubts about my question, please ask in the comments.
try it
$ string = "Hello! [Number: 0] "; $ Array = array (); $ Array [0] = array ('name' = & gt; "no"); $ Array [1] = array ('name' = & gt; "nam"); Echo preg_replace ("# (\!)? \ S + \ [num: (\ d +) \] # i.e. ',' isset ($ array [\ 2])?" \ 1 ". $ Array [\ 2] ["Name"]: "" ', $ string);
Comments
Post a Comment