Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112864 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 61612 invoked from network); 13 Jan 2021 11:12:38 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Jan 2021 11:12:38 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1C1181804F2 for ; Wed, 13 Jan 2021 02:50:50 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from adsar.co.uk (adsar.co.uk [138.68.176.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 13 Jan 2021 02:50:49 -0800 (PST) Received: from DESKTOPRRF7EQB (host86-157-214-84.range86-157.btcentralplus.com [86.157.214.84]) (authenticated bits=0) by adsar.co.uk (8.14.4/8.14.4) with ESMTP id 10DAmo5n012856 for ; Wed, 13 Jan 2021 10:48:50 GMT DKIM-Filter: OpenDKIM Filter v2.11.0 adsar.co.uk 10DAmo5n012856 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adsar.co.uk; s=default; t=1610534931; bh=s+bcPjed11O2QSPl2Y365iEYnjhqdFAyXVgSFDEE51M=; h=From:To:References:In-Reply-To:Subject:Date:From; b=CaGQjqNiyIqOe0zNgxuxuVTVXoYhqowBpuXexmKDQ76KfrH3MxwAfz3nTRB8iyEhX P6ooAocQCDbHpAjxaMLREiAzb+YYXBcG/cPVSerXePV9K0tas63/Qcq/UJ4Pz65EX0 FLZhcTac1kRcMMWFQDRQF+UMdsZ79HXfTeL6tOoI= To: References: In-Reply-To: Date: Wed, 13 Jan 2021 10:50:49 -0000 Message-ID: <046401d6e999$f4de5d50$de9b17f0$@adsar.co.uk> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQKdd6XTqVhBQ6IRLEMJmwXndifW4KiYQ9/g Content-Language: en-gb X-Antivirus: Avast (VPS 210113-0, 13/01/2021), Outbound message X-Antivirus-Status: Clean Subject: Addition of substring and subistring functions. From: adam@adsar.co.uk ("Adam Cable") Hi internals. I've been coding in PHP for 15 years now, and spend most days using it to transform content into meaningful data. Most of this is pulling out prices and attributes of certain products from HTML, for example, grabbing the price from content such as "Total price including delivery: £15.00". To grab the "15.00" from the string can take quite a few lines of PHP and can be pretty cumbersome. I've built some helper functions - substring, and it's case-insensitive variant subistring to help. Functions take in the string plus a to and from string, and return a trim'd string found between the two. So substring("Total price including delivery: £15.00", "£", "") would return "15.00". From and to strings are optional and therefore return from the beginning or to the end. In the past I hadn't thought about adding this to PHP core, but with the introduction of str_starts/ends_with functions in PHP 8.0 I thought it may be useful to include the sub(i)string building blocks too. Implementation and tests can be found @ https://github.com/php/php-src/pull/6602 I'm sure the C implementation can be made a lot better, but it seems to work OK at present. This is my first e-mail to internals, so please excuse my naivety with things, but hope this is useful. Thanks, Adam