MYSQL Trim function using on PHP [duplicate] - php

This question already has answers here:
Php trim string at a particular character
(7 answers)
Closed 8 years ago.
I have a mysql codes which i need to do that trim and leading with PHP, anyone know how to write below logics with PHP code.
LEFT(TRIM(LEADING 'DHL ' FROM CONT_NAAM),20)
TRIM(LEADING 'DHL JVGL' FROM CONT_NAAM)

You can use preg_replace:
substr(preg_replace('/^(DHL )+/', '', $cont_naam), 1, 20)

Related

String Function in PHP [duplicate]

This question already has answers here:
PHP - Return everything after delimiter
(7 answers)
How to get everything after a certain character?
(9 answers)
get substring after delimiter
(3 answers)
Get the string after a string from a string
(5 answers)
Closed 3 years ago.
String Function in PHP:
$img = "WhatsApp Image 2019-11-18 at 17.15.42.jpeg | xyz.com/content/uploads/wcpa_uploads/image.jpg";
using string functions, how can I get complete text after | symbol, I mean only the complete url of image should be saved in variable.
There are several methods for that. If you do not want to regex or split to array, try this:
$path = trim(substr(strstr($img, "|"), 1));
Simplest way:
trim(explode('|', $img)[1])

How to decode Ascii code in a Character in PHP [duplicate]

This question already has answers here:
URL Decoding in PHP
(6 answers)
Closed 6 years ago.
I got the string in this format
solr/?key=color&facet=Blue%26keyword%3Dwoo
However, I want to get it in this format
solr/?key=color&facet=Blue&keyword=woo
Try urldecode:
$url = urldecode("solr/?key=color&facet=Blue%26keyword%3Dwoo");
// = solr/?key=color&facet=Blue&keyword=woo

Php, return portion of string before "0" [duplicate]

This question already has answers here:
How do I remove all specific characters at the end of a string in PHP?
(10 answers)
Closed 6 years ago.
I search PHP method for return portion of string before "0" has no number (1-9) after.
Example :
604000
I want to PHP script return :
604
Can you help me ?
Just use rtrim() as
$str="604000";
echo $str = rtrim($str, '0');

Eregi_replace to preg_replace conversion needed [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
i need to convert this in order to use preg_replace because eregi_replace is deprecated
$n=eregi_replace(")+.+","",$value);
$id=eregi_replace(".+)","<a hсref='http://www.company.com/product.php?id=".$n."'>",$value);
$newstr.="".$id."</a><br>";
//..... must be converted
its very unclear to me how that would look in preg_replace
$gexpieces = explode(")", $value);
$n=$gexpieces[0];//n is product id
unset($gexpieces[0]);
$prodname = implode($gexpieces);
$newstr.="<a href='http://www.company.com/product.php?id=".$n."'>".$prodname."</a><br/><br/>";
nevermind, done it myself.

How to find 3 or more of a character in a string? [duplicate]

This question already has answers here:
How do I count comma-separated values in PHP?
(5 answers)
Closed 9 years ago.
$string = "oidjdssd , odi,jdois, 3089u,, oisdjsd";
How do i find out if theres more than 3 commas in the string above in the best way?
I would suggest substr_count. You can see if the result is >3 to see if there's more than three.
echo count_chars($string)[ord(',')];
Or for PHP<5.4
$chars = count_chars($string);
echo $chars[ord(',')];
BTW: As it seems, that you are handling CSV-data, you should have a look at str_getcsv()

Categories