For the backstory I am looking to update my search script a little. Initially I wanted to be able to search the ID column of a particular table, but in order to search a column set in integer format I need to cast it as character so that I can search it like text.
I had forgotten about it until I updated my search program to search a "date" column that was set in datetime format and discovered that I was getting an odd number of results. After looking into it more closely I soon realized that I needed to cast the date column as char or cast(date as char). I'm using the pcre I listed to generate a small part of a query for the search script I use on my site.
I tried using str_replace to replace the instances of date and date2, but if I replace date I also replace date2. It also does not account for whether the occurrence occurs at the beginning, middle, or end of the string.
Another option which hadn't occurred to me is to use explode and then do a search and replace of all of the occurrences of date and date2 in the array, but I suspect I'd still have to jump through another hoop just to replace the occurrences of date rather than date2.
So that leaves me with my current situation. It works now, but I am having trouble simplifying my pcre, which is not really necessary, but it seems like I should be able to do it. When I used your script as well as the way I tried it earlier it wasn't remembering the location of the commas in my string.
The string is a long list of column names separated by a comma. Here is an example of my string that I need to format:
Code:
$clmns="ID,title,body,ID,title,basic_premise,summary,conclusion,ID,title,summary,ID,ID,verse,ID,ID,summary,ID,ID,summary,ID,ID,date,summary,ID,title,summary,date,date2";
$clmns2="title,body,title,basic_premise,summary,conclusion,title,summary,ID,verse,ID,summary,ID,summary,ID,date,summary,title,summary,date,date2";
After being formatted it should look like this:
Code:
$clmns="cast(ID as char),title,body,cast(ID as char),title,basic_premise,summary,conclusion,cast(ID as char),title,summary,cast(ID as char),cast(ID as char),verse,cast(ID as char),cast(ID as char),summary,cast(ID as char),cast(ID as char),summary,cast(ID as char),cast(ID as char),cast(date as char),summary,cast(ID as char),title,summary,cast(date as char),cast(date2 as char)";
$clmns2="title,body,title,basic_premise,summary,conclusion,title,summary,cast(ID as char),verse,cast(ID as char),summary,cast(ID as char),summary,cast(ID as char),cast(date as char),summary,title,summary,cast(date as char),cast(date2 as char)";?>
The full code snippet I am currently using is:
Code:
$clmns2 = str_replace("ID","cast(ID as char)",$clmns2);
$clmns = str_replace("ID","cast(ID as char)",$clmns);
$clmns2 = preg_replace('/^date,/',"cast(date as char),",$clmns2);
$clmns2 = preg_replace('/,date,/',",cast(date as char),",$clmns2);
$clmns2 = preg_replace('/,date$/',",cast(date as char)",$clmns2);
$clmns2 = preg_replace('/^date2,/',"cast(date2 as char),",$clmns2);
$clmns2 = preg_replace('/,date2,/',",cast(date2 as char),",$clmns2);
$clmns2 = preg_replace('/,date2$/',",cast(date2 as char)",$clmns2);
$clmns = preg_replace('/^date2,/',"cast(date2 as char),",$clmns);
$clmns = preg_replace('/,date2,/',",cast(date2 as char),",$clmns);
$clmns = preg_replace('/,date2$/',",cast(date2 as char)",$clmns);
$clmns = preg_replace('/^date,/',"cast(date as char),",$clmns);
$clmns = preg_replace('/,date,/',",cast(date as char),",$clmns);
$clmns = preg_replace('/,date$/',",cast(date as char)",$clmns);
Bookmarks