Merge two functions, str_replace?

Asked
Active3 hr before
Viewed126 times

7 Answers

functions
90%

Placing a function in a function should work. Otherwise, copy the str_replace from func3 to func 2 before the return line. – Martijn Dec 19 '17 at 9:58 ,Tried to place one function inside another, but that didn't work.,Wrap above in single function you don't need n str_replace() calls to replace n characters,I need to replace som characters when importing data from a feed. I'm not sure how I can merge two functions into one to make an url correct.

You could replace several characters using single call by passing array in str_replace

str_replace(array("Aéryne", " "), array("Aeryn", "-"), $string);
load more v
88%

The str_replace() is a built-in function in PHP and is used to replace all the occurrences of the search string or array of search strings by replacement string or array of replacement strings in the given string or array respectively. ,$subjectVal: This parameter can be of both string and array types. This parameter specifies the string or array of strings which we want to search for $searchVal and replace with $replaceVal.,$replaceVal: This parameter can be of both string and array types. This parameter specifies the string with which we want to replace the $searchVal string.,$searchVal: This parameter can be of both string and array types. This parameter specifies the string to be searched and replaced.

Syntax

str_replace($searchVal, $replaceVal, $subjectVal, $count)

Examples:  

Input: $subjectVal = "It was nice meeting you. May you shine brightly."
str_replace('you', 'him', $subjectVal)
Output: It was nice meeting him.May him shine brightly.

Input: $subjectVal = "You eat fruits, vegetables, fiber every day."
$searchVal = array("fruits", "vegetables", "fiber")
$replaceVal = array("pizza", "beer", "ice cream")
str_replace($array1, $array2, $str)
Output: You eat pizza, beer, ice cream every day.
Output:
It was nice meeting him.May him shine bright.
Output:
You eat pizza, beer, ice cream every day.
load more v
72%

Example #2 Examples of potential str_replace() gotchas, This function returns a string or an array with all occurrences of search in subject replaced with the given replace value. , This function returns a string or an array with the replaced values. ,Example #1 Basic str_replace() examples

load more v
65%

echo str_replace("worss", "world", "Hello worss in PHP!!");
load more v
75%

The second function removes the spaces of a custom field,The first function hides the last X characters of a custom field,I'm trying to get 2 php functions with my custom field to work together with a custom field. they both work by themselves great but I can't get them to work together.,@prosti is right, if you're looking for a function though, you can wrap both calls in a single reusable function.

I had the same problem.

echo substr($custom_field, 0, -6);
echo str_replace(' ', '', $custom_field);

I think I solved it this way.

echo substr(str_replace(' ', '', $custom_field), 0, -6);
load more v
40%

In this example, the str_replace() function replaces the substring 'hi' with the string 'bye' in the string 'Hi, hi, hi'.,In this example, the str_ireplace() function replaces the substring 'hi' or 'Hi' with the string 'bye'.,The following example uses the str_replace() function to replace the substring 'bye' with the string 'hey' in the string 'bye bye bye':,The following example uses the str_replace() function to replace the string 'Hello' with 'Hi' in the string 'Hello there':

The following shows the syntax of the str_replace() function:

.wp - block - code {
      border: 0;
      padding: 0;
   }

   .wp - block - code > div {
      overflow: auto;
   }

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;
   }

   .hljs {
      box - sizing: border - box;
   }

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;
   }

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;
   }

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;
   }

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;
   }

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;
   }

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;
   }

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;
   }

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;
   }

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
   }
str_replace(
   array | string $search,
   array | string $replace,
   string | array $subject,
   int & $count = null
): string | arrayCode language: PHP(php)
load more v
22%

str_replace_all(string, pattern, replacement),Vectorised over string, pattern and replacement.,To replace the complete string with NA, use replacement = NA_character_.,str_replace_na() to turn missing values into "NA"; stri_replace() for the underlying implementation.

Usage

str_replace(string, pattern, replacement) str_replace_all(string, pattern, replacement)
load more v

Other "functions-undefined" queries related to "Merge two functions, str_replace?"