php remove special characters [duplicate] - php

This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 7 years ago.
I have a string like this in the database
!80!n!98!n!22!n!6!n!76!n!1!n!24!n!129!n!59!n!73!n!7!n!40!n!85!n!35!n!42!n!126!n!81!n!37!n!128!n!147!n!106!n
I want to remove those ! and n symbols and want to add a pipe in between characters
80|98|22|6|76|1|24|129|59|73|7|40|85|35|42|126|81|37|128|147|106

you could update the column first:
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, '!n!', '|')
WHERE SomeOtherColumn LIKE '%PATTERN%'
More information on replace here:
How can I use mySQL replace() to replace strings in multiple records?

<?php
$str="!80!n!98!n!22!n!6!n!76!n!1!n!24!n!129!n!59!n!73!n!7!n!40!n!85!n!35!n!42!n!126!n!81!n!37!n!128!n!147!n!106!n
";
$str1 = preg_replace('/\s+/','|',str_replace(array("!n","!","!n!"),' ',trim($str)));
echo $output = substr($str1, 1, -1);
OUTPUT
80|98|22|6|76|1|24|129|59|73|7|40|85|35|42|126|81|37|128|147|106

Related

PHP array_pop & implode show question mark when used with foreign language [duplicate]

This question already has answers here:
Getting the first character of a string with $str[0]
(9 answers)
Closed 2 years ago.
I'm using the following code to display only the last name first character on my Wordpress site:
$name = $comment->comment_author;
$separate = mb_split(" ", $name);
$last = array_pop($separate);
echo implode(' ', $separate)." ".$last[0].".";
It works great with English names, but $last[0] will return a question mark when used with a foreign language (e.g Arabic, Hebrew, Greek etc). For example:
Name: השם שלי
Will return:
השם ?.
I've been trying to fix this for an hour now, but nothing so far.
Any idea?
You can try to set the encoding (https://www.php.net/manual/ro/function.mb-internal-encoding.php) before using the mb_split function.

Retrieving the First n letters from table in sql [duplicate]

This question already has answers here:
Truncate a string to first n characters of a string and add three dots if any characters are removed
(20 answers)
Closed 7 years ago.
I saw this post which had my ans: MySQL Select Query - Get only first 10 characters of a value
How to do the same using php ?
This can be done by substr function. Following is the working example with code.
http://sugunan.net/demo/substr.php
<?php
$full_str = "Hello, this is my subject and how are you";
$firstn = substr ( $full_str , 0 ,10 );
echo $firstn;
?>
Use the substr() function. Read about it here http://php.net/manual/en/function.substr.php
$shortString = substr($longString,$startPoint,$numberOfCharactersToReturn);
eg: substr('abcdefg',0,3);
// returns 'abc'

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)

Trim result is unexpected? [duplicate]

This question already has answers here:
php trim(), work poorly
(2 answers)
Closed 9 years ago.
$individual_file["uri"] = "public://iStock_000000527255XSmall.jpg";
print_r(ltrim($individual_file["uri"], "public://"));
Result -: Stock_000000527255XSmall.jpg
Why the missing i? But when my character starts with si, I get si in the result. Why does trim behave differently?
$individual_file["uri"] = "public://siStock_000000527255XSmall.jpg";
print_r(ltrim($individual_file["uri"], "public://"));
Result -: siStock_000000527255XSmall.jpg
It's because charlist is literally a list of single characters to remove from the left side of the string and i is listed in public://. Any character that falls in this list will be removed, no matter the order.
Ref: http://php.net/manual/en/function.ltrim.php
In fact this:
$individual_file["uri"] = "public://iStock_000000527255XSmall.jpg";
print_r(ltrim($individual_file["uri"], "publc://"));
would output:
ic://iStock_000000527255XSmall.jpg
Another example by changing the order:
$individual_file["uri"] = "public://iStock_000000527255XSmall.jpg";
print_r(ltrim($individual_file["uri"], "bcilpu:/"));
would output:
Stock_000000527255XSmall.jpg

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