This is my sample preg_match and working, but how to add Comma "," on code already like bellow
$content = explode(" ",$title);
$result = "";
foreach($content as $value){
if(!preg_match("/\.|'/",$value)){
$find = mysqli_query($db, "select * from table where column1='$value'");
$j = mysqli_num_rows($find);
if($j>0){
$ka = mysqli_fetch_array($find);
$result = $result.$ka['column2'];
}else{
$find2 = mysqli_query($db, "select * from table where column2='$value'");
$j2 = mysqli_num_rows($find2);
if($j2>0){
$ka2 = mysqli_fetch_array($find2);
$result = $result.$ka2['column1']);
}else{
$result = $result.$value;
}
}
}else{
}
i just try like this but not work
if(!preg_match("/\.\,|'/",$value)){
blablabla
}else{
blablabla
}
because I am still learning php so confused what to do, thank you if anyone would like to help
I assume you want to match a list of characters, use the character class [] instead like:
if(!preg_match("/[.,']/",$value)){
blablabla
}else{
blablabla
}
Learning regex is invaluable, read up on it and it'll pay for itself in a month!
Related
This question already has answers here:
PHP Mysql WHILE loop remove the last separator
(4 answers)
Closed 6 years ago.
I'm trying to send mulitple sms and the correct format for the api to send the text messages should be (00,001,002) but with my current sql it produces this results (00,001,002,)
How can i correct my script to get this results (00,001,002)
$query_rs_p = "SELECT * FROM contacts ORDER BY id DESC";
$rs_p = mysql_query($query_rs_p, $insurance) or die(mysql_error());
$row_rs_p = mysql_fetch_assoc($rs_p);
$totalRows_rs_p = mysql_num_rows($rs_p);
$ph=',';
do {
echo $row_rs_p['phone_number'].$ph;
} while ($row_rs_p = mysql_fetch_assoc($rs_p));
Instead of echoing it out in your loop, put it all in a string. Then trim off the last comma before you output it:
$query_rs_p = "SELECT * FROM contacts ORDER BY id DESC";
$rs_p = mysql_query($query_rs_p, $insurance) or die(mysql_error());
$row_rs_p = mysql_fetch_assoc($rs_p);
$totalRows_rs_p = mysql_num_rows($rs_p);
$ph=',';
$string = '';
do {
$string .= $row_rs_p['phone_number'].$ph;
} while ($row_rs_p = mysql_fetch_assoc($rs_p));
$string = rtrim($string, ',');
echo $string;
Or, better yet, just build this string in your query. Much simpler and cleaner:
$query_rs_p = "SELECT group_concat(phone_number) as phine_numbers FROM contacts ORDER BY id DESC";
$rs_p = mysql_query($query_rs_p, $insurance) or die(mysql_error());
$row_rs_p = mysql_fetch_assoc($rs_p);
echo $row['phone_numbers'];
Use rtrim function
$rowsphone = $row_rs_p['phone_number'].$ph;
rtrim($rowsphone,',');
Probably the most direct would be to just do something like this:
$output = '';
do {
if ($output != '')
$output .= ',';
$output .= $row_rs_p['phone_number'];
while ($row_rs_p = mysql_fetch_assoc($rs_p);
echo $output;
I have a mysql table(request) here:
request:
r_id item_no
1 1
13 10
22 20
33 30
55 40
Now, I'm using php to take r_id out using mysql_fetch_array() and try to put mysql_fetch_array() result in to a string also split the string by comma.
<?php
include('config.php'); //connect to mysql
function f(){
$sql = "SELECT r_id FROM `request` ";
$result=mysql_query($sql);
$string = '';
$i = 0;
while ($row = mysql_fetch_array($result)) {
$string .= $row[$i];
}
$newstring = implode(", ", preg_split("/[\0-9]+/", $string));
return $newstring ;
}
$get=f();
echo $get;
?>
But I cannot get the right string form my php code.
I want to get the string like
1,13,22,33,55
How can I fix the problem?
You don't need to split that and implode, just do this:
while ($row = mysql_fetch_array($result)) {
$string .= $row[$i].',';// append comma after each value you append to the string
}
return substr($string,0,-1);// cut off the trailing comma from the final string
<?php
include('config.php'); //connect to mysql
function f(){
$sql = "SELECT GROUP_CONCAT(r_ID) FROM `request` ";
$result=mysql_query($sql);
return $result ;
}
$get=f();
echo $get;
?>
Try my updated answer:
$sql = "SELECT r_id FROM `request` ";
$result=mysql_query($sql);
$string = '';
$i = 0;
while ($row = mysql_fetch_array($result)) {
$string .= $row[$i].",";
}
return $string ;
}
$array = array();
while(...){
$array = $row[$i]; //first you need array
}
$string = implode(",",$array); //now you have your string
ORIGINAL CODE
$sentance="are you hungry too?";
function newLanguage($text) {
$sql = "SELECT in,out FROM words";
$res = mysql_query($sql) or die();
$in_array = array();
$out_array = array();
while ($row = mysql_fetch_array($res)){
$in_array[] = $row['in']; // table in, NEW words
$out_array[] = $row['out']; //table out, ENG words
}
return preg_replace($in_array,$out_array,$text);
}
$newwords = newLanguage($sentance);
echo $newwords;
AMENDED CODE:
ini_set("display_errors", "1");
error_reporting(E_ALL);
function newLanguage($text) {
$sql = "SELECT in,out FROM words";
$res = mysql_query($sql) or die();
$in_array = array();
$out_array = array();
while ($row = mysql_fetch_array($res)){
$in_array[] = '/\b' . preg_quote($row['in']) . '\b/'; // table in, NEW words
$out_array[] = $row['out']; //table out, ENG words
}
/* VERSION 2 - STATIC, FOR DEBUGGING
$in_array = array('~\you~s','~\to~s','~\too~s');
$out_array = array('noa','nie','niee');*/
return preg_replace($in_array,$out_array,$text);
}
$sentance="are you hungry too?";
$newwords = newLanguage($sentance);
var_dump($in_array);
echo $sentance;
CURRENT CODE
ini_set("display_errors", "1");
error_reporting(E_ALL);
$sentance="are you hungry too?";
function newLanguage($text) {
$sql = "SELECT * FROM words";
$res = mysql_query($sql) or die();
while ($row = mysql_fetch_array($res)){
$in_array[] = '/\b' . preg_quote($row['in']) . '\b/'; // table in, NEW words
$out_array[] = $row['out']; //table out, ENG words
}
return preg_replace($in_array,$out_array,$text);
}
$newwords = newLanguage($sentance);
var_dump($in_array);
echo $newwords;
I'm having Problems with my code, for the life of me I cannot get it to work, I have worked with preg_replace before in using a word filter. Though creating a dynamic array using query results totally throws me off. I have looked at a few tutorials but none have really helped me understand where I am going wrong.
Any help would be grateful :)
GOAL:
Creating a New Language translation. Database holds a row with 'in' & 'out' which is both the new language and local language.
PROBLEM:
I'm unsure if my Arrays are being successfully populated since my preg_replace isn't working.
----UPDATE----
Here is what my database looks like;
id in out
1 you noa
2 to nie
3 too niee
They are stored as VARCHARS
Try this:
while ($row = mysql_fetch_array($res)){
$in_array[] = '/\b' . preg_quote($row['in']) . '\b/'; // table in, NEW words
$out_array[] = $row['out']; //table out, ENG words
}
This turns the in worods into regular expressions, adding \b to match word boundaries.
My whole test code is:
<?php
$sentance="are you hungry too?";
function newLanguage($text) {
$in_array = array();
$out_array = array();
$rows = array(array('in' => 'you', 'out' => 'noa'),
array('in' => 'to', 'out' => 'nie'),
array('in' => 'too', 'out' => 'niee'));
foreach ($rows as $row) {
$in_array[] = '/\b' . preg_quote($row['in']) . '\b/'; // table in, NEW words
$out_array[] = $row['out']; //table out, ENG words
}
return preg_replace($in_array,$out_array,$text);
}
$newwords = newLanguage($sentance);
echo $newwords;
The $rows array replaces the database query, but the rest is essentially the same.
BIG thank you to Barmar for his dedicated help and patience in helping me solve my question!
I have made his answer correct since he helped me so much but if you wish to use my final code that is commented please feel free.
My Goal was to IMPORT database rows and insert them into an Array which will be used with preg_replace.
My Final Code was:
//this is the base text that will be modified using database information
$sentance="are you hungry too?";
function newLanguage($text) {
$sql = "SELECT * FROM words";
$res = mysql_query($sql) or die();
$in_array = array();
$out_array = array();
while ($row = mysql_fetch_array($res)){
// inserts the column row 'in' into an array called in_array
$in_array[] = '/\b' . preg_quote($row['in']) . '\b/';
// inserts the column row 'out' into an array called out_array
$out_array[] = $row['out'];
}
//replaced any matching words from 'sentance' with 'in_array' and replaces with 'out_array'
return preg_replace($in_array,$out_array,$text);
}
//this makes a new variable and used a function to replace the text with matches in the variable
$newwords = newLanguage($sentance);
echo $newwords;
I have text :
$a = I wanna eat apple , and banana .
I wanna get every words and punctuation of that sentence :
$b = explode(' ', strtolower(trim($a)));
the result of explode is array.
I have a words table on db that has fields : id, word and typewords all in lowercase. but for punctuation there are no exist in db.
I wanna search every words in db to take the type of words, so the final result that i want to get is :
words/typeofwords = I/n wanna/v eat/v apple/n ,/, and/p banana/n ./.
here's the code :
function getWord ($word){
$i = 0 ;
$query = mysql_query("SELECT typewords FROM words WHERE word = '$word' ");
while ($row = mysql_fetch_array($query)) {
$word[$i] = $row['typewords'];
$i++;
}
return $word;
}
echo $b.'/'.getWord($b);
but it doesn't work, please help me, thanks !
Try with this:
function getWord($words){
$association = array();
foreach($words as $word)
{
$query = mysql_query("SELECT typewords FROM words WHERE word = '$word' ");
if($row = mysql_fetch_array($query))
$association[$word] = $row['typewords'];
elseif(preg_match('/[\.\,\:\;\?\!]/',$word)==1)
$association[$word] = $word;
}
return $association;
}
$typewords = getWord($b);
foreach($b as $w)
echo $w.'/'.$typewords[$w];
function getWord($word){
// concat the word your searching for with the result
// http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
$query = mysql_query("SELECT CONCAT(word,'/',typewords) as this_result FROM words WHERE word = '$word' ");
$row = mysql_fetch_array($query);
return $row['this_result']." "; // added a space at the end.
}
// loop through the $b array and send each to the function
foreach($b as $searchword) {
echo getWord($searchword);
}
You assume the function parameter to be an array, but it is a string.
Edit: In your function you treat $word as an array as well as a string. Decide what you want and recode your function.
I am printing a set of words that is placed in a MySQL database and I am retrieving it with PHP. I want to present it as a comma separated list, but I need it not to print or remove the last comma. How could I do this?
I did try to use rtrim, but I did not probably do it right.
This is my code as it is today:
<?php
$query0 = "SELECT LCASE(ord) FROM `keywords` ORDER BY RAND()";
$result0 = mysql_query($query0);
while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC))
{
$keyword = $row0['LCASE(ord)'];
echo "$keyword, ";
?>
I did try to use rtrim, my attempt was something like this (I might be honest enough to say that I am in above my head in this ;) )
$keyword = $row0['LCASE(ord)'];
$keywordc = "$keyword, ";
$keyword- = rtrim($keywordc, ', ');
echo "$keyword-, ";
As you might imagine, this did not print much (but at least it did not leave me with a blank page...)
I would do:
$keywords = array();
while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC))
{
$keywords[] = $row0['LCASE(ord)'];
}
echo implode(',', $keywords);
I usually do this by placing the results in an array first
$some_array = array();
while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC)) {
$some_array[] = $row0['LCASE(ord)'];
}
then simply:
echo "My List: " . implode(', ', $some_array);
// Output looks something like:
My List: ord1, ord2, ord3, ord4
substr($string, 0, -1);
That removes the last character.