Answers for "php remove from end of string"

PHP
2

remove word from string php

<?php
// IF ONLY CHARACTER IN STRINGS
$str = "Hello PHP";
echo str_replace("PHP","",$str); // its remove PHP From string
//Output = Hello;

// IF NUMARIC OR CHARACTERS IN STRING AND YOU NEED ONLY STRING
$strInt = "124Peter56";
echo $words = preg_replace('/[0-9]+/', '', $strInt); // it remove integer from string
// Output = Peter;

// IF NUMARIC OR CHARACTERS IN STRING AND YOU NEED ONLY INTEGERS
$strInt = "124Peter56";
echo $res = preg_replace("/[^0-9]/", "", $strInt ); // It Remove all string 
// Output = 12456
?>
Posted by: Guest on March-26-2022
3

php remove everything after character

$fullpath = 'folderName/file.ext';
$folder = substr($fullpath, 0, strpos($fullpath, '/'));
echo $folder;
// Output => folderName
Posted by: Guest on March-10-2020
1

php remove stop words from string

public function optimizeSearchString($searchString = "")
  {
    $stopwords = array(
      'der' => 1,
      'die' => 1,
      'das' => 1,
      'the' => 1);

    $words = preg_split('/[^-\w\']+/', $searchString, -1, PREG_SPLIT_NO_EMPTY);

    if (count($words) > 1) {
      $words = array_filter($words, function ($v) use (&$stopwords) {
        return !isset($stopwords[strtolower($v)]);
      }
      );
    }

    if (empty($words)) {
      return $searchString;
    }

    return implode(" ", $words);
  }
Posted by: Guest on September-02-2020
-1

php remove control characters from string

preg_replace('/[[:cntrl:]]/', '', $input);
Posted by: Guest on June-29-2021

Code answers related to "php remove from end of string"

Browse Popular Code Answers by Language