CakePhp, how MySQL "Like" works as case insensitive search [duplicate] - php

This question already has answers here:
How can I search (case-insensitive) in a column using LIKE wildcard?
(16 answers)
Closed 8 years ago.
In CakePhp, how MySQL Like works as case insensitive search.
I have tried with following code but problem is also coming here.
If i am searching "Motel Park" then result is fine.
But when searching "motel park" no result is found.
In database Collation is latin1_swedish_ci
$where_condition = array('Ad.completed' => 1,
'Ad.address LIKE '=>'%'.$_REQUEST['address'].'%',
'Ad.status' =>$ad_status
);
$result = $this->Ad->find('all',array('conditions'=>$where_condition));

You can convert both the value to lowercase to use LIKE statement
$where_condition = array(
'Ad.completed' => 1,
'LOWER(Ad.address) LIKE '=>'%'.strtolower($this->request->data['address']).'%',
'Ad.status' =>$ad_status
);
$result = $this->Ad->find('all',array('conditions'=>$where_condition));

try using "COLLATE utf_general_ci" collation. refer this link.
but there is an easy solution , by converting all string in to lowercase. then check.
$request_address=strtolower($_REQUEST['address']);
$where_condition = "Ad.completed = 1 AND
LOWER(Ad.address) LIKE '%".$request_address."%' AND
Ad.status ='".$ad_status."'";
$result = $this->Ad->find('all',array('conditions'=>$where_condition));

Related

PHP replace text inside specific text [duplicate]

This question already has answers here:
Parse Wordpress like Shortcode
(7 answers)
Closed 4 years ago.
I'm using str_replace to replace a simple shortcode which works fine:
$content = "[old_shortcode]";
$old_shortcode = "[old_shortcode]";
$new_shortcode = "[new_shortcode]";
echo str_replace($old_shortcode, $new_shortcode, $content);
However I want to also replace attributes inside the shortcode without affecting any text content, for example change this:
[old_shortcode old_option_1="Text Content" old_option_2="Text Content"]
To this:
[new_shortcode new_option_1="Text Content" new_option_2="Text Content"]
Much appreciated if anyone could advise on how to do this.
To clarify, this question is not about parsing a shortcode (as it has been marked as a duplicated), it's about replacing one shortcode with another which the duplicate question linked to does not answer.
Edit:
I figured it out myself, however it's probably not a very elagant solution if anyone wants to suggest something better?
$pattern1 = '#\[shortcode(.*)attribute1="([^"]*)"(.*)\]#i';
$replace1 = '[shortcode$1attribute1_new="$2"$3]';
$pattern2 = '#\[shortcode(.*)attribute2="([^"]*)"(.*)\]#i';
$replace2 = '[shortcode$1attribute2_new="$2"$3]';
$pattern3 = '#\[shortcode(.*)(.*?)\[/shortcode\]#i';
$replace3 = '[new_shortcode$1[/new_shortcode]';
$content = '[shortcode attribute2="yes" attribute1="whatever"]Test[/shortcode]';
echo preg_replace(array($pattern1,$pattern2,$pattern3), array($replace1,$replace2,$replace3), $content);
Use preg_replace() instead that select only part of string you want using regex.
$newContent = preg_replace("/[a-zA-Z]+(_[^\s]+)/", "new$1", $content);
Check result in demo

MySQL - Searching for Swedish Characters

I have stored Swedish words in MysQL database. The encoding of the table is
utf8_unicode_ci
I also tried
utf8_swedish_ci
when I'm doing a search in database, 'a' and 'ä' are treated the same. So the results include both words that begin with a and ä. This is true vice-versa as well.
My application is developed with CakePHP and I have the following in my core.php
Configure::write('App.encoding', 'UTF-8');
Am I missing anything here?
This is my query:
$term = $this->request->query['term'];
$this->loadModel('Words');
$condition = array('word LIKE' => trim($term) . '%', 'is_active' => true);
$words = $this->Words->find('list', array(
'fields' => array('word'),
'limit' => 7,
'conditions' => $condition));
return $this->respond($words, true);
I use to for Jquery autocomplete:
$(function () {
$("#tags").autocomplete({
source: 'autocomplete'
});
});
$query_result = $this->Words->query("SELECT word FROM tbl_words WHERE word LIKE '" . $term . "%' COLLATE utf8_bin LIMIT 7 ;");
This works great for me. Thanks #Mihai for your comment.
I had similar issue. I wanted to list all the autocomplete suggesstions by a tag which may contain swedish characters. I had already a right charset in the mysql database. So no issues there. But I was still failing to search properly from the mysql database with a swedish tag name. However, it turns out to be not related to mysql. I missed to decode the tag in my php backend.
$term = urldecode($this->request->query['term']);
That makes sense, because the jquery autocompate sends out the tag in the url.

Replace multiple elements in one string [duplicate]

This question already has an answer here:
How do I use str_replace() with multiple parameters?
(1 answer)
Closed 8 years ago.
I have the following string: "English,French,German".
Replacing one element in this string is no problem, doing this:
#if (strpos($language,'English') !== false)<dd>{{{ $malestr = str_replace("English", "Eng.", $language)}}}</dd>#endif
Resulting in: "Eng.,French,German"
Now the problem is I want to replace all three elements and return only one string, but I don't know how to get to this. Can you help?
--
edit: thanks to Mahammad Alabed, found the solution in your link. This worked:
{{{$result = str_replace(
array('English', 'French', 'German'),
array('Eng.', 'Fr.', 'Ge.'),
$language
)}}}
$languages = array(
'English' => 'Eng.',
'French' => 'Fre.',
'German' => 'Ger.',
);
$malestr = str_replace(array_keys($languages), array_values($languages), $language);
This will replace all the terms you're looking for.

Get values between tags and convert them as link [duplicate]

This question already has answers here:
Check string for words starting with the character #
(2 answers)
PHP replace link in tags like [LINK]
(2 answers)
Closed 9 years ago.
I have a site where I would like to get a value from textarea on submit and replace it wih a link, e.g. a user submitted Hello #testuser how are you?
I want to get the content between # and space and replace it with
testuser
I searched for it a lot but couldn't find anything to work with. I also did it without dynamic content. The script is:
<?php
$tagOne = "[";
$tagTwo = "]";
$text = "Hello, my name is [lol]";
$uname = "lol";
$replacement = "".$uname."";
$startTagPos = strrpos($text, $tagOne);
$endTagPos = strrpos($text, $tagTwo);
$tagLength = $endTagPos - $startTagPos + 1;
$text = substr_replace($text, $replacement, $startTagPos, $tagLength);
echo $text;
?>
This is perfect if I only have a username defined in the code but not the user. I want the same result but dynamically.
If you have a database, make a users table, then connect to the database in the file you're working with, and query out the user based on the id and assign it to a variable ex.($username). Then you can replace the "my name is '[lol]' " to "my name is '$username' ". I suppose this would work, if i understood your question right.

Case sensitive , add strtolower [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Users to register only lower case letters
Hi, I have this register page , and i would like to have the script register only lower case letters : pretty
I do not want it to register :Pretty , PRETTy , PRETTY ... Here is the code , what do i need to add to it ?
public function addField($field_name){
if (!array_key_exists($field_name, $this->fields))
{
if ($field_name=='username') {
$field = new field_join_username();
parent::registerField($field);
}
if ($field_name=='email') {
$field = new field_join_email();
parent::registerField($field);
}
}
parent::addField($field_name);
}
Where's that code taken from? Are you trying to change existing code?
As far as I can see, this only adds fields not values of fields where the strtolower should be applied, so applying strtolower to field_name won't do the trick ... but it's hard to guess where to place strtolower without seeing the correct code snippet.

Categories