How can I replace commas with brakes in select option to change from this:
To this:
It seems that my current code doesent work anymore:
// $row[1] = "enum('1','2','3','4','5','6','7','8','9','10')";
$options = explode("', '", preg_replace("/(enum|set)\('(.+?)'\)/", "\\2", $row[1]));
Full code from first image:
$row = $db->query("SHOW COLUMNS FROM namemap LIKE 'multiplier'");
$row = $row->fetch_row();
$options = explode("', '", preg_replace("/(enum|set)\('(.+?)'\)/", "\\2", $row[1]));
print("<tr><td align='left' class='header'>Multiplier:</td>");
print("<td align='left' class='lista' colspan='2'><select name='multiplier'>");
foreach($options as $multiplier) {
$option = "<option ";
if ($multiplier == $results['multiplier'])
$option .= "selected=selected ";
$option .= "value=".$multiplier.">" . unesc($multiplier) . "</option>";
print($option);
}
print("</select></td></tr>");
Here is how I separated the enum string. Not the most elegant thing, but it works:
$options = explode(",",str_replace("'","", str_replace('enum(', '', str_replace(')','', $row[1]))));
Also I got rid of the print() statements. You can output the html directly - separating the html presentation from the php logic helps make the code a little more legible and flexible.
<?php
$row = array("multiplier","enum('1','2','3','4','5','6','7','8','9','10')","YES","","1","");
$options = explode(",",str_replace("'","", str_replace('enum(', '', str_replace(')','', $row[1]))));
?>
<tr><td align='left' class='header'>Multiplier:</td>
<td align='left' class='lista' colspan='2'><select name='multiplier'>
<?php foreach($options as $multiplier) {
$s='';
if ($multiplier == $results['multiplier']) $s="selected";
?>
<option value='<?php echo $multiplier?>' <?php echo $s?>><?php echo $multiplier?></option>
<? } ?>
</select></td></tr>
I think you found the correct place where it is not working any longer:
$options = explode("', '", preg_replace("/(enum|set)\('(.+?)'\)/", "\\2", $row[1]));
The reason is that the split-string "', '" contains a space after the comma while the string returned from preg_replace does not have spaces after the comma.
So using explode with a fixed string does not suffice any longer as entering these values in the database may be with or without space(s) after a comma (and perhaps even before?).
Once seen, the fix could be straight forward. One solution is to use a regular expression pattern as well when "exploding", there is a sister function for that with preg_replace and it is called preg_split:
$options = preg_split("/',\s*'/", ...);
^^^
\s : space character
* : zero or more, as many as possible (greedy)
So exchanging explode with preg_split can upgrade the code to handle cases with zero or more space characters after the comma.
Likewise you could introduce potential space characters also before the comma by allowing them the same, just before the comma.
Related
how can i get only text data from the table? Without any links and images?
while ($row = $most_rep->fetch_assoc()) {
$post_title = $row['post_title'];
$post_content = mb_substr($row['post_content'],0,120);
$guid = $row['guid'];
$post_date = $row['post_date'];
echo
"<a href='$guid' target='_blank'>"
."<div class='post_title'>$post_title</div>"."</a>"
."<a href='$guid' target='_blank'>"
."<div class='post_description'>$post_content</div>"
."<div class='post_date'>$post_date</div>"
."</a>";
}
Inside variable $post_content i have text and images and links from the post. I would like to echo only text.
Appreciate any help
use 'strip_tags' function
http://php.net/manual/en/function.strip-tags.php
for example ,
$only_text = strip_tags($post_content);
it is not the best practice but if you still insist using this kind of approach i suggest add some Delimiter to your data $row['post_content'] and use explode(" ",$row['post_content']) to get the specific text or value that you want.
Note: the explode function will return an array.
You can use PHP Filter Sanitization:
<?php
$str = "test";
$newstr = filter_var($str, FILTER_SANITIZE_STRING);
echo $newstr;
?>
Try this to remove everything except a-z, A-Z and 0-9:
$result = preg_replace("/[^a-zA-Z0-9]+/", "", $s);
If your definition of alphanumeric includes letters in foreign languages and obsolete scripts then you will need to use the Unicode character classes.
Try this to leave only A-Z:
$result = preg_replace("/[^A-Z]+/", "", $s);
The reason for the warning is that words like résumé contains the letter é that won't be matched by this. If you want to match a specific list of letters adjust the regular expression to include those letters. If you want to match all letters, use the appropriate character classes as mentioned in the comments.
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo " {$row['id']}. ". "{$row['name']} <br> ".
"Music : {$row['lljhg2']} <br> ".
"Video : {$row['description']} <br> ".
"Song : {$row['artist']} <br> ".
"--------------------------------<br>";}
Some code Like this, I want {$row['name']} in <a href=\"/load/{$row['id']}/{$row['name']}\"> I want this row results all spaces replaced by dash ('-').
If the result is:
Prince Reoy from USA
I want to print this:
prince-reoy-from-usa
Is it possible?
either use this query
select lcase(replace(name, ' ', '-')) from table_name
or this php code
strtolower(' ', '-', str_replace($row['name']))
This will replace spaces with dashes, and make the whole string lower case:
strtolower(str_replace(' ', '-', $row['name']))
You can use implode() which will add the "glue" (the '-') between pieces of the array to make a string. Then you can lower the case of the string all at once with strtolower():
$row_data = strtolower(implode('-', $row));
echo $row_data;
I have some code running which finds out hashtags in the string and turns them into links. I have done this using preg_match_all as shown below:
if(preg_match_all('/(#[A-z_]\w+)/', $postLong, $arrHashTags) > 0){
foreach ($arrHashTags[1] as $strHashTag) {
$long = str_replace($strHashTag, ''.$strHashTag.'', $postLong);
}
}
Also, for my search script, I need to bold the searched keywords in the result string. Something similar to the below code using preg_replace:
$string = "This is description for Search Demo";
$searchingFor = "/" . $searchQuery . "/i";
$replacePattern = "<b>$0<\/b>";
preg_replace($searchingFor, $replacePattern, $string);
The problem that I am having is that both have to work together and should be thrown as a combined result. One way I can think of is to run the resultant string from preg_match_all with the preg_replace code but what if the tags and the searched string are the same? The second block will bold my tag as well which is not desired.
update the code i'm running based on the answer given below but it still doesn't work
if(preg_match_all('/(#[A-z_]\w+)/', $postLong, $arrHashTags) > 0){
foreach ($arrHashTags[1] as $strHashTag) {
$postLong = str_replace($strHashTag, ''.$strHashTag.'', $postLong);
}
}
And immediately after this, i run this
$searchingFor = "/\b.?(?<!#)" . $keystring . "\b/i";
$replacePattern = "<b>$0<\/b>";
preg_replace($searchingFor, $replacePattern, $postLong);
Just so you know, this is all going inside a while loop, which is generating the list
You just need to modify you the search pattern to avoid ones that start with a '#'
$postLong = "This is description for Search Demo";
if(preg_match_all('/(#[A-z_]\w+)/', $postLong, $arrHashTags) > 0){
foreach ($arrHashTags[1] as $strHashTag) {
$postLong = str_replace($strHashTag, ''.$strHashTag.'', $postLong);
}
}
# This expression finds any text with 0 or 1 characters in front of it
# and then does a negative look-behind to make sure that the character isn't a #
searchingFor = "/\b.?(?<!#)" . $searchQuery . "\b/i";
$replacePattern = "<b>$0<\/b>";
preg_replace($searchingFor, $replacePattern, $postLong);
Or if you don't need an array of the available hashes for another reason, you could use preg_replace only.
$postLong = "This is description for #Search Demo";
$patterns = array('/(#[A-z_]\w+)/', "/\b.?(?<!#)" . $searchQuery . "\b/i");
$replacements = array(''.$0.'', ' "<b>$0<\/b>');
preg_replace($patterns, $replacements, $postLong);
Probably a simple problem here, but I cannot find it.
I am exploding a string that was input and stored from a textarea. I use nl2br() so that I can explode the string by the <br /> tag.
The string explodes properly, but when I try to get the first character of the string in a while loop, it only returns on the first line.
Note: The concept here is greentexting, so if you are familiar with that then you will see what I am trying to do. If you are not, I put a brief description below the code sample.
Code:
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$comment = nl2br($row['comment']);
$sepcomment = explode("<br />", $comment);
$countcomment = count($sepcomment);
$i = 0;
//BEGIN GREENTEXT COLORING LOOP
while($i < $countcomment) {
$fb = $sepcomment[$i];
$z = $fb[0]; // Check to see if first character is >
if ($z == ">") {
$tcolor = "#789922";
}
else {
$tcolor = "#000000";
}
echo '<font color="' . $tcolor . '">' . $sepcomment[$i] . '</font><br>';
$i++;
}
//END GREENTEXT COLORING LOOP
}
Greentext: If the first character of the line is '>' then the color of that entire line becomes green. If not, then the color is black.
Picture:
What I have tried:
strip_tags() - Thinking that possibly the tags were acting as the first characters.
$fb = preg_replace("/(<br\s*\/?>\s*)+/", "", $sepcomment[$i]);
str_replace()
echo $z //Shows the correct character on first line, blank on following lines.
$z = substr($fb, 0, 1);
Here is a test I just did where I returned the first 5 characters of the string.
Any ideas for getting rid of those empty characters?
Try "trim" function
$fb = trim($sepcomment[$i]);
http://php.net/manual/en/function.trim.php
(probably line breaks are the problem, there are \n\r characters after tag)
I'm trying to build a string of HTML as follows:
$html= "<input name='".GROUP_CONFIG_MAX_CALL_RECORDING_TIME_INPUT."' value='".$MaxCallRecordingTimeSecs."' size='4' ".($bCallRecordingLicensed)?'':'disabled'.">";
But it just gives me a parse error (no specific detail, just that this line is the problem).
I've tried various positioning of quotations and brackets but I'm always getting the parse error. Is this possible the way I'm trying?
$html= "<input name='".GROUP_CONFIG_MAX_CALL_RECORDING_TIME_INPUT."' value='".$MaxCallRecordingTimeSecs."' size='4' ".($bCallRecordingLicensed?'':'disabled').">";
Like codingbiz said, this should work with additional parentheses. I'd go for a more readable version with sprintf though:
$html = sprintf(
'<input name="%s" value="%s" size="4"%s>',
GROUP_CONFIG_MAX_CALL_RECORDING_TIME_INPUT,
$MaxCallRecordingTimeSecs,
( $bCallRecordingLicenced ? '' : ' disabled' )
);
Try
".(($bCallRecordingLicensed)?'':'disabled').">";
additional brackets
Try wrapping the whole ternary in parens, rather than just the variable at the start:
$html= "<input name='".GROUP_CONFIG_MAX_CALL_RECORDING_TIME_INPUT."' value='".$MaxCallRecordingTimeSecs."' size='4' ".($bCallRecordingLicensed?'':'disabled').">";
I think its because you change quote marks:
For example
$test = false;
$strings = "hello ".($test?"you":"")." this is a test";
echo $strings;
works as you expected.
I did find that without the brackets round the test, for example, my test only produced the word "you" .. rather than the whole string - which was odd.