Giter Club home page Giter Club logo

php-useful-functions's Introduction

PHP-useful-functions

A repo with useful PHP functions

str_replace_between

Even after looking for it for a while, I didn't find anything doing exactly what I needed so I created this function.

This function replaces a string between 2 given strings, with the possibility to keep or remove the given strings.

Description

string str_replace_between ( string $haystack , mixed $replace , mixed $needleStart , string $needleEnd , bool $keepNeedleStart , bool $keepNeedleEnd, bool $keepAllNeedleStart)

Returns part of haystack string with strings between needleStart and needleEnd replaced by replace.

Arguments

haystack The input string.

replace The replacement value that replaces string between needleStart and needleEnd.

needleStart The beginning value to search for in haystack.

needleEnd The ending value to search for in haystack.

keepNeedleStart If true, occurrences of needleStart will be kept in haystack.

keepNeedleEnd If true, occurrences of needleEnd will be kept in haystack.

keepAllNeedleStart If true, occurrences of needleStart with no needleEnd after will be kept.

Example

$haystack = '1FooIsAwesome_And2FooIsFun3FooIsSmart_And4FooKnowsEverything_AndOver';
$needleStart = 'Foo';
$needleEnd = '_And';
$keepNeedleStart = true;
$keepNeedleEnd = false;
$keepAllNeedleStart = true;

echo str_replace_between($haystack, 'Bar', $needleStart, $needleEnd, $keepNeedleStart, $keepNeedleEnd, $keepAllNeedleStart);

// Will return 1FooBar2FooFooBar4FooBarOver

php-useful-functions's People

Contributors

etienneperriere avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.