Removing particular string with special character and number - php

I have following example its working fine but i want to remove Issue No1 – ,Issue No2 – ,Issue No3..... at once because I don't want to add Issue No2 – , Issue No3 ... everytime in preg_replace. I think this is possible with preg_replace. I wrote following code but did not get the actual result. Any advise or guidance would be greatly appreciated.
$catName="Issue No1 – This is new Issue No2 – This is also new item Issue No3 – This is new one There are 1 2 3 items ....... ";
echo $catName= preg_replace("/Issue No\d+/","",$catName);
Output should be:
This is new This is also new item This is new one There are 1 2 3 items .......

You also have to add - at end of your expression
preg_replace("/Issue No\d+ –/", "", $catName);
DEMO

Try like below -
$catName = preg_replace("(Issue No[0-9]+)", "", $catName);

<?php
$catName="Issue No1 - This is new Issue No2 - This is also new item Issue No3 - This is new one There are 1 2 3 items ....... ";
echo "<h1>$catName</h1>\n";
$catName = preg_replace("/Issue No\d+ - /","",$catName);
echo "<h1>$catName</h1>\n";
?>

Related

PHP: "While" loop returns same value for each iteration of the variable

Thanks in advance for any help offered.
I'm performing a query that returns something like this:
route | busnumber
2 300
4 123
2 455
12 934
My goal is to print/echo a list of all "busnumber" were "route" = 2.
I know I can do a query on the DB to do this but I don't want to run this query for each and every route. The result set is actually part of one master, complex query so I need to accomplish my goal using the array.
The query works correctly and does display the appropriate info. I also return the results into an array. i.e ...
$query=("SELECT * from foo")
$result=mysql_fetch_assoc($query);
My current code looks like this (note my comments):
mysql_data_seek( $result,0); // because I'm previously iterating through $result
while ($i2=mysql_fetch_assoc($result)) {
$busnumber=$i2['busnumber'];
$busroute=$i2['route'];
echo "<div class='businfo'>";
if ($busroute='2'){ // I only want this to happen if $busroute=2
echo "Current Route = $busroute</br>";
echo "Bus Number : $busnumber </br>";
}
echo "</div>";
}
What I'm getting, however, it is echoing the same '$busroute' (2) but different '$busnumber' for each row as such:
Current Route = 2
Bus Number : 194
Current Route = 2
Bus Number : 196
Current Route = 2
Bus Number : 2002
Current Route = 2
Bus Number : 2010
Current Route = 2
Bus Number : 2013
The problem is that all of those bus numbers are not part of route 2. It is a listing of bus numbers from all routes. I only want it to perform the foreach loop if $routenumber=2.
Thanks again for the help :)
if ($busroute='2'){
The problem is that you're not checking whether it's equal to 2 here, you're assigning it to equal 2. Change it to this and you'll be fine:
if ($busroute=='2'){
$query = "SELECT * from foo";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
if ($row['route'] == 2) {
// do your stuff
}
}

PHP + PDO, my escaped characters won't show when displayed

First, I'd like to apologize, I'm still a beginner.
For learning purposes I'm creating a blog engine and I just noticed that when I list the comments, escaped characters like carriage return (pressing enter) are shown in the database correctly, but only a whitespace character when displaying the comments.
I'm using PostgreSQL 8.3.
Here you can see an example database entry:
fema=> select * from comments where id = 54;
-[ RECORD 1 ]-------------------------
id | 54
authorid | 1
text | This new line won't show.\r
: No\r
: \r
: new\r
: \r
: \r
: \r
: lines.
time | 1341417673
postid | 15
answerid | 0
Here you can see what var_dump() shows:
string(50) "This new line won't show. No new lines."
This is how I'm getting the data:
$stmt = db::$db->prepare("select comments.id as commentid ,authorid,text,time,postid,answerid,users.* from comments left join users on users.id = comments.authorId where postid = :postId");
$stmt->execute(array('postId' => $_GET['p']));
$commentRslt = $stmt->fetchAll();
Then foreach to iterate through them and replace the mark I'm using to identify things I have to replace:
$currComment = str_replace('{{{cms:comment:text}}}', $commentRslt[$key]['text'], $currComment);
This is how I insert the new comment to the DB:
$stmt = self::$db->prepare('INSERT INTO comments (authorId, text, time, postId, answerId) VALUES (:authorId, :text, :time, :postId, :answerId)');
$stmt->execute(array( 'authorId' => $_SESSION['userId'],
'text' => str_replace(array('<','>'), array('&lt','&gt'), isset($_POST['newCommentArea']) ? $_POST['newCommentArea'] : $_SESSION['newCommentArea']),
'time' => time(),
'postId' => isset($_POST['commentNew']) ? $_POST['commentNew' ] : $_SESSION['postId'],
'answerId' => $answerId));
Sorry for the many code samples, but I don't even know where the problem is and I wanted to be thorough.
So could anyone please tell me how to solve the problem? If only just by telling me where I made the mistake. I really have no clue.
Thanks.
Jut guessing here, but:
Consecutive whitespace is collapsed to a single space by HTML/the browser. Replace newlines with <br> tags if you want to keep them, using nl2br.

PHP - Count number of times string appears in file

I've got a file (leaderboard.txt) that looks like this:
funkystudios
funkystudios
funkystudios
gilletteracer74
axehairgel
Ferby123
dirdam
TheWu13
Expert_Assassin
TheWu13
ocanosoup
I want to be able to read this file, and print out the number of times each person appears in the file. (Also place in order of # of times in file)
funkystudios: 3
TheWu13: 2
gilletteracer74: 1
axehairgel: 1
(and so on)
I've tried various ways but It all came down to an issue when I would try to order them correctly... I'm guessing there is a pretty easy way to do this. (I'm new to PHP...)
EDIT:
I have gotten to this point:
foreach(array_count_values(file('leaderboard.txt')) as $person => $count)
echo "{$person} : {$count}<br />\r\n";
It doesn't order by the $count, but simply who comes up first in the file.
$counted = array_count_values(file('leaderboard.txt'));
arsort($counted);
foreach($counted as $person => $count)
echo "{$person} : {$count}<br />\r\n";

How to retrieve 1 record at a time from db

I have a table comment as follow:
Comment
comment_id cmt followupid
1 Hello 3
2 hi 4
3 Hey 2
4 wassup 1
My query is that I want to echo "Hello", "hi", "hey" , "Wassup" and other (the record continues) individualy, I have used
$comment = mysql_result($runQuery, $i,"cmt");
echo $comment;
which works fine but the problem is that it echoes all the comments at once, what I want is to echo all the comment but one at a time. the comment are in a div tag such that each the div appears only after 1 second the page is loaded. I want each comment to appear after different time interval
for e.g:
Hello to appear at 5pm (not necessarily the corect time it can be just an int)
hi 5.10 pm
hey 6.30 pm
Please Help!
The following code should give you some hints.
$result = mysql_query($runquery);
while($row=mysql_fetch_assoc($result)){
// $row contains a single row.
echo $row['cmt'], $row['comment_id']
}
Create another variable storing time divisions(or number of rows). So that different output at different time can be fetched. For eg. If your table has 24 rows(or items), then this variable shall have a value 24. Use it to divide you output times(As in 24 hours, each hour a different value).
Now, the PHP part(I am not much familiar with date and time functions in PHP, so you can totally ignore the paragraph above.
$result = mysql_query($runquery);
$j = 0;
$i = rand( 0, mysql_num_rows($result) );
while($row=mysql_fetch_assoc($result)){
// $row contains a single row.
if( $j++ = $i )
echo $row['cmt'], $row['comment_id'];
}
This will fetch one random row from the table, but not depending upon the server time.

MySql Query adding number 1 to variable when used in query?

Hope you can help I have a simple query updating positions x and y based various user id etc. But I have a problem when I pass the variable to be updated (through ajax) to PHP, I get the variable fine but on placing it in a query a number 1 is added to the query end making the last id unusable (see example id 68 becomes 681).
Never seen this before, I am relatively new to sql tho, hope someone can shed some light on this?
$xupdate = $_POST['xupdate'];
$yupdate = $_POST['yupdate'];
$stickytext_id = $_POST['stickytextid'];
$user_id= $_POST['uid'];
$proj_id=$_POST['projid'];
echo $xupdate; //output 358
echo'<br>';
echo $yupdate; //output 203
echo'<br>';
echo $stickytext_id; //output 68
echo'<br>';
echo $proj_id; //output 7
echo'<br>';
$sql_update_stickyxy="UPDATE textsticky SET textsticky_x = $xupdate AND textsticky_y = $yupdate
WHERE textsticky_id = $stickytext_id";
echo $sql_update_stickyxy; //outputs UPDATE textsticky SET textsticky_x = 358 WHERE textsticky_id = 681 not 68?
Looking at your echo'd output you obviously embezzled some of your code. As a first debugging measure you might use $_POST['stickytextid'] instead of $stickytext_id inside your query and see where it gets you.

Categories