PHP retrieve MySQL result based on nth letter(s) [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to retrieve some results from the database based on some letters known.
Here is my PHP script:
$sql = "SELECT * FROM lexem WHERE charLength = '$total'";
$doh = "__a_d";
$sql .= " AND formUtf8General LIKE '$doh'";
The length, letters, letters number and letters position are variable.
My script only returns results that contain some or one of my letters. How can I improve it?

I hope that i have understood correctly. I really want you to try the code below. It uses an array containing both letters and their position in the SQL parameter.
$filter_string = '';
$offset = 0;
$total = 10; //or any other number
$escaped_characters = array('_', '%'); //Any other MySQL special characters
$filters = array(
3=>'a', 5=>'d', 9=>'%', 11=>'r', 0=>'n', 2=>'k', 1=>'w', -1=>'t',
);
ksort($filters);
foreach($filters as $key => $value) {
if ($key < 1 || strlen($value) != 1) {
continue;
}
for($i = 1; $i < ($key - $offset); $i++) {
$filter_string .= '_';
}
if (in_array($value, $escaped_characters)) {
$filter_string .= '\\'.$value;
} else {
$filter_string .= $value;
}
$offset = $key;
}
//Code that escapes parameters in query
$sql = "SELECT * FROM lexem WHERE charLength = '$total' AND formUtf8General LIKE '$filter_string%';";
The $filters array is the one that has the positions of the characters in a key => value format (one character each time). We need to sort it by key so the offset variable will take the correct values each time in the foreach loop. In case you need characters that are also MySQL special characters then $escaped_characters array is useful.
Important: You have to escape all query parameters before you run queries like that because of security reasons (SQL Injection).
Additional info: How can I prevent SQL injection in PHP?

Related

for loop - print values in order - PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to print the below statement.
Thanks for updating your PH_NUMBER, ADDRESS, OFFICE NUMBER and EMAIL.
Where PH_NUMBER, ADDRESS, OFFICE_NUMBER and EMAIL are variables.
For example, if value is empty for variable "OFFICE_NUMBER" the sentence should be like below:
Thanks for updating your PH_NUMBER, ADDRESS and EMAIL.
For example, if value is empty for variable "EMAIL" the sentence should be like below:
Thanks for updating your PH_NUMBER, ADDRESS and OFFICE NUMBER.
$a = "Address";
$b = "Mobile";
$c = "email";
$array = array($a, $b, $c);
$length = count($array);
for ($i = 0; $i < $length; $i++) {
$total = $array[$i];
}
echo "Thanks for udpating " . $total ;
How can this be done ?
Any suggestions would be helpful.
You don't need to use a loop.
<?php
/* Collect all parameters to the array */
$variables = [
'PH_NUMBER',
'ADDRESS',
'EMAIL',
'OFFICE_NUMBER',
];
/* Sorting the array if need */
/* Pop the element off the end of array */
$variables = array_filter($variables);
$count = count($variables);
$lastVariable = array_pop($variables);
if($count > 1) {
printf('Thanks for updating your %s and %s', implode(', ', $variables), $lastVariable);
}elseif($count === 1){
printf('Thanks for updating your %s', $lastVariable);
}else{
print 'Nothing for update';
}
?>
Update: Small improvement in order to catch cases with only 1 variable and empty array (nothing for update).

Permutation in order algorithm if anyone could help me with the loop coding [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Hi there I was wondering if I could create an array that accept a sentence and turns it into an array for example
$sentence = 'my name is john'
then the array will be:
$arr = {my name is john,
my name is, my name john, my is john, name is john,
my name, my is, my john, name is, name john, is john,
my, name, is, john}
anyone could help me with the implementation in any kind of loop that would be great because im currently creating a simple search engine algorithm thx :D
Think of it as a n-bit integer, with each bit corresponding to whether a word is included or not in a given string in your array. Loop from 1 to (1 << n) - 1, which in this case is 1 to 15, to get your 15 word lists, and for each one, check each of the bits in the integer and add the corresponding word if the corresponding bit is set:
function getCombinations($sentence)
{
$words = explode(" ", $sentence);
$combinations = array();
for($i = 1; $i < (1 << count($words)); $i++)
{
$wordlist = "";
for($j = 0; $j < count($words); $j++)
{
if($i & (1 << $j))
{
$wordlist = $wordlist . " " . $words[$j];
}
}
array_push($combinations, substr($wordlist, 1));
}
return $combinations;
}
$a = "my name is john";
print_r(getCombinations($a));
If you want your strings to be sorted by the number of words, add an extra loop for the word counts:
for($wordcount = count($words); $wordcount >= 1; $wordcount--)
{
for($i = 1; $i < (1 << count($words)); $i++)
{
if(NumberOfSetBits($i) == $wordcount)
{
$wordlist = "";
// generate word list..
}
}
}
NumbeOfSetBits function from here

insert comma into string in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
how do i insert a comma in string like
$str = "0470 06102009 2981485GIR ADE TAUHID";
every length i put in array like
$legth = array(7,8,15,50);
i just wanna make the result like
0470 ,06102009 ,2981485GIR ,ADE TAUHID
where every string splited according to length on the array, including whitespace,
length(7),length(8),length(15),length(50)
how i do that ?
I would do something like this:
$offset = 0;
$result = implode(",",array_map(function($length) use ($str,&$offset) {
$part = substr($str,$offset,$length);
$offset += $length;
return $part;
},$legth));
Demo: http://ideone.com/ISWZuO
Please note that the output of the demo is what you "should" get based on the input you provided. Your input is wrong for the output you say you want - adjust $legth as needed.
if you are sure of the number of spaces and you want to keep them, you can use substr
// substr( your string, start, length )
substr($str , 0, 7)
This will work for you:
$oldString = "0470 06102009 2981485GIR ADE TAUHID";
$newstring = implode(", ", preg_split("/[\s]+/", $oldString));
echo $newstring;
EDIT:
If you need output on the basis of your array. Then I think you need to modify your array first. And apply below code:
$legth = array(7,8,15,50);
$str = "0470 06102009 2981485GIR ADE TAUHID";
$first = 0;
foreach($legth as $l){
echo substr($str , $first, $l)."<br />";
$first = $first + $l;
}

Append a number to every php instance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a form in my website where users can send messages to multiple recipients, they type in the recipient's numbers separated by commas, I recieve the numbers in my php script as a single post variable, but I need to add the recipient,s area code on front of every number, I have tried to explode the variable using the comma as a separator but I do not know what to do from there
$recipient=$_POST['recipient'];
$numbers=explode(',' $recipient);
for ($i = 0; $i <=sizeof($numbers); $i++) {
}
I do not know how you're getting the area codes in the script, but to prepend the number with the area code, you can just use string concatenation, like so:
$areacode = 123; // this is received dynamically
$result = array(); // initialize empty array
for ($i = 0; $i <= sizeof($numbers); $i++) {
$result[] = $areacode . '_' . $numbers[$i];
}
This can also be done using a foreach:
foreach (explode(',', $recipient) as $number) {
$result[] = $areacode . '_' . $number;
}
Now, if you want to get this back as a comma-separated string, you can use implode(), like so:
$result_string = implode(',', $result);
The short answer is something like:
$numbers_with_area_codes = Array();
for ($i = 0; $i <= sizeof($numbers); $i++) {
$numbers_with_area_codes[] = '+372'.$numbers[$i];
}

Looking for improvement on PHP split and combination function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Suppose I have a string such as this:
ABC.DEF.GHI.JKL (This string could have any length and amount of characters between dots)
I want to add the following combinations into an array.
ABC
ABC.DEF
ABC.DEF.GHI
ABC.DEF.GHI.JKL
So basically the string should be split with the dot character and then the individual sub-strings should be combined.
I have a function already, but in my opinion this seems too complicated to achieve the result.
$myarray = array();
$split = explode('.', 'that.string.with.dots');
$string = '';
for ($i = 0; $i < count($split); $i++) {
$string .= $split[$i];
myarray[] = $string;
$string .= '.';
}
Any suggestions on improving this?
Thanks
Michael
Perhaps this:
$split = explode('.', 'that.string.with.dots');
for ($i = 1; $i < count($split); $i++) {
$split[i] = $split[$i-1] . '.' . $split[i];
}
It just concats the current with the previous.
I have the feeling that you are asking if there is a PHP function that will achieve this result with a single function call, but I don't think there is one.
You can rewrite the code as pritaeas did in his answer, to remove two lines of code, but it will still be the same concept.

Categories