Eregi_replace to preg_replace conversion needed [duplicate] - php

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.

Related

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

MYSQL Trim function using on PHP [duplicate]

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)

PHP Array of Some Kind [duplicate]

This question already has answers here:
What is PHP's serialize() function for?
(4 answers)
Closed 9 years ago.
I come across that array-looking object in a php file. Any clue what it is and how it works?
a:1:{i:2;a:2:{i:0;s:1:"m";i:1;s:10:"2013-02-11";}}
What you have is a serialized string. Run it through unserialize to convert it into a usable format.
Replace $string in the example below.
$unserialized = unserialize( $string );

eregi() deprecated - I am not sure how to rewrite this code [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
How to validate an Email in PHP?
(7 answers)
How to validate an email address in PHP
(15 answers)
Closed 9 years ago.
Deprecated code:
function validate_email($email)
{
return eregi("^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email);
}
I am a JavaScript beginner. The above code gives an error. I am not too sure how to rewrite using preg_match.
Try this instead
function validate_email($email)
{
return (1 === preg_match("/^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email));
}
preg_match() should be used rather than eregi. You must include the slashes at the begining and end of the pattern. And finally, the "/i" at the end of the pattern makes it case-insensitive.

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