Return the Portion of a String Before the First Occurrence of a Character in PHP

Return the portion of a string before the first occurrence of a character in PHP

You could do this:

$string = 'The quick brown fox jumped over the lazy dog';
$substring = substr($string, 0, strpos($string, ' '));

But I like this better:

list($firstWord) = explode(' ', $string);

PHP substring extraction. Get the string before the first '/' or the whole string

Use explode()

$arr = explode("/", $string, 2);
$first = $arr[0];

In this case, I'm using the limit parameter to explode so that php won't scan the string any more than what's needed.

PHP: how to add substring right before first occurrence of another string?

Try a combination of strpos and substr_replace ?

$strOne = "Place new content here:  , but not past the commma.";
$strTwo = "test content";

// find the position of comma first
$pos = strpos($strOne, ',');
if ($pos !== false)
{
// insert the new string at the position of the comma
$newstr = substr_replace($strOne, $strTwo, $pos, 0);
var_dump($newstr);
}

Output:

string(63) "Place new content here: test content, but not past the
commma."

PHP get everything in a string before underscore

You should simple use:

$imagePreFix = substr($fileinfo['basename'], 0, strpos($fileinfo['basename'], "_"));

I don't see any reason to use explode and create extra array just to get first element.

You can also use (in PHP 5.3+):

$imagePreFix = strstr($fileinfo['basename'], '_', true); 

Using PHP to Get Character Before the First Period?

you can use strpos and substr functions
strpos - it'll return index of first occurrence of substring
substr - return specific part of string

$domain='example.com';
$lastchar=substr($domain, strpos($domain,'.')-1, 1);

PHP First occurance of string before and after index

As a starting point, you can try:


$input = 'a.b.something.c.def';
$index = 9;
$delimiter = '.';

/*
* get length of input string
*/
$len = strlen($input);

/*
* find the index of the first delimiter *after* the index
*/
$afterIdx = strpos($input, $delimiter, $index);

/*
* find the index of the last delimiter *before* the index
* figure out how many characters are left after the index and negate that -
* this makes the function ignore that many characters from the end of the string,
* effectively inspecting only the part of the string up to the index
* and add +1 to that because we are interested in the location of the first symbol after that
*/
$beforeIdx = strrpos($input, $delimiter, -($len - $index)) + 1;

/*
* grab the part of the string beginning at the last delimiter
* and spanning up to the next delimiter
*/
$sub = substr($input, $beforeIdx, $afterIdx - $beforeIdx);
echo $sub;

Note, at the very minimum you'll need to add some sanity checking for cases where there's no symbol before/after the index.

  • strlen
  • strpos
  • strrpos
  • substr

Isolate leading portion of string before first hyphen and omit any trailing spaces from match

Use explode() with the "-" as a delimiter to split it into chunks based on the presence of the "-" and then take the first portion and use trim() to trim the trailing spaces to give the title with no trailing spaces. This gives "THIS IS A TEST" in all the provided test cases. If there are no dashes then the entire string is returned.

<?php

$str = 'THIS IS A TEST - 10-01-2010 - HELLO WORLD (OKAY)!!';
$title= explode('-', $str);
$trimmedTitle=trim($title[0]);
print_r($trimmedTitle);

//$trimmedTitle ='THIS IS A TEST

?>

I have tested this against:

THIS IS A TEST - 10-01-2010 - HELLO WORLD (OKAY)!!
THIS IS A TEST-10-01-2010 - HELLO WORLD (OKAY)!!
THIS IS A TEST - - - - 10-01-2010 - HELLO WORLD (OKAY)!!
THIS IS A TEST

and each returns THIS IS A TEST with no trailing spaces

PHP script to remove everything before the first occurrence of a number

If you want to call the function like you mentioned in your post you can do like the below:

<?php
function removeEverythingBefore($value, $pattern) {
preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE);
$initialPosition = $matches[0][1];
return substr($value, $initialPosition);
}

$value = "Hello I want to rule the world in 100 hours or so";
$value = removeEverythingBefore($value, '/[0-9]/');
echo $value; // prints 100 hours or so

This way you can use the same function to match other patters aswell.



Related Topics



Leave a reply



Submit