Current Code:
<?php
include('conn.php');
$sql = mysql_query("select min(news_id) 'min' from news");
$res = mysql_fetch_array($sql);
$qry=mysql_query("select * from news order by date(post_date) desc,priority desc ");
while($row1=mysql_fetch_array($qry)) {
<p class='news_title'><a href='newsdetail.php?id=".urldecode($row1['news_heading'])."'>{$row1['news_heading']}</a></p>
}
?>
As I am passing the heading to the next page ...
The url in the next page is displaying like this
newsdetail.php?id=In%20front%20%20of%20the%20houses
I need to display like this:
newsdetail.php?id=In front of the houses
You can use urldecode to convert the URL string.
http://www.php.net/manual/en/function.urldecode.php
echo urldecode('newsdetail.php?id=In%20front%20%20of%20the%20houses');
will produce:
newsdetail.php?id=In front of the houses
You can't have spaces or special characters in the URL.
If you try to put them in, then the browser will put %20 for spaces.
for clean url please replace the space or special charcters with hypen(-)
function cleanURL($textURL) {
$URL = strtolower(preg_replace( array('/[^a-z0-9\- ]/i', '/[ \-]+/'), array('', '-'), $textURL));
return $URL;
}
while($row1=mysql_fetch_array($qry)) {
<p class='news_title'><a href='newsdetail.php?id=".cleanURL($row1['news_heading'])."'>{$row1['news_heading']}</a></p>
}
The output will be
newsdetail.php?id=In-front-of-the-houses
Think so this will help you
You can't have raw spaces in the URL. If you try to put them in, then the browser will error correct and escape them for you.
use this
$url = str_replace(' ', '-', strtolower($news_heading));
replace the space of this function
Related
I need to display title from referring URL and here is the code I'm using to achieve that:
<?php
if (isset($_SERVER['HTTP_REFERER'])) {
$url_to_load = $_SERVER['HTTP_REFERER'];
$f = file_get_contents($url_to_load);
$p1 = strpos($f, "<title>");//position start
$qe = substr($f, $p1);//string from start position
$p2 = strpos($qe, "</title>");//position end
$query = substr($qe, 7, $p2-2);//cuts from start position +7 (<title>) untill end position -2...
echo $query;}
else{
$ref_url = 'No Reffering URL'; // show failure message
}//end else no referer set
echo "$ref_url";
?>
When i visit page with this code from URL that has the following code:
<title>Title Of Referrer</title>
Code works, but there is still the piece of the closing tag and when i check source code this is what i'll get:
Title Of Referrer</tit
What i need to change to remove the closing tag completely?
$query = substr($qe, 7, $p2-7);//cuts from start position +7 (<title>) untill end position -2...
You only subtract 2 at the end on end title but you add 7 on start title.
Try the code above and see if that works
EDIT:
Another solution is to do like this.
$query = strip_tags(substr($qe, 0, $p2));
This saves all of the title tags but then delete them with strip_tags()
EDIT2:
There are some other things in the code I would suggest.
$f = file_get_contents($url_to_load);
$query = strip_tags(substr($f, strpos($f, "<title>"), strpos($f, "</title>")));
This code brings it down to two lines of code and uses fewer variables. You can also get ridd of $f, but it may be useful to something else and it's only one variable.
Okai, so my problem is that one of my URL's get unvalid because my SQL table outputs a space before and after the actuall URL string.
I added some pictures and some code for guidance.
<a href="..">
<img src="../
<?php
$fetchheader = $db->prepare("SELECT background FROM header ORDER BY id DESC");
$fetchheader->execute();
$headerall = $fetchheader->fetchAll();
$fetchheader->execute();
echo $headerall[0]['background'];
?>
">
</a>
Here are some pictures aswell to explain the problem:
Here you can see that the type is set to varchar:
Here you can see that the output on the websites causes errors:
You have left empty spaces between HTML and <?php/?>, what is reason you got in your image path, so you should remove these spaces:
<img src="../<?php
$fetchheader = $db->prepare("SELECT background FROM header ORDER BY id DESC");
$fetchheader->execute();
$headerall = $fetchheader->fetchAll();
$fetchheader->execute(); // <-- probably this is not necessary :)
echo $headerall[0]['background'];
?>">
can you try it?
use trim() to remove unwanted spaces in php
$background=trim($headerall[0]['background']);
or
$background=$headerall[0]['background']; preg_replace('/\s+/', '', $background);
If you want to replace the blank space in MYSQL
UPDATE `table` SET `col_name` = REPLACE( `col_name` , ' ' , '' )
I want to remove %, +, ascii codes from url.
Example:
From
http://prexprint.com/Laminated%20Business%20Cards
to
http://prexprint.com/Laminated Business Cards
Browser will always render in URL spaces with %20 we can't change it.
if you want to change it http://prexprint.com/Laminated Business Cards than instead of this make your url
http://prexprint.com/Laminated+Business+Cards
or
http://prexprint.com/LaminatedBusinessCards
$x = 'http://prexprint.com/Laminated%20Business%20Cards';
$y =str_replace('%20',' ',$x);
echo $y;
or use
<?php echo rawurldecode('http://prexprint.com/Laminated%20Business%20Cards'); ?>
URLs in address bar cannot be with spaces. You can use URL Rewrite such that you can make your URL look like this http://prexprint.com/Laminated-Business-Cards. Even if you place a link like this http://prexprint.com/Laminated Business Cards, the browsers will automatically replaces the spaces with '%20'
There you go with JS:
var orgUrl = 'http://prexprint.com/Laminated%20Business%20Cards';
var reqUrl = decodeURI(orgUrl);
console.log(reqUrl);
Edit:
var orgUrl = 'http://prexprint.com/Laminated%20Business%20Cards';
var reqUrl = decodeURI(orgUrl)
reqUrl = reqUrl.replace(/\ /g, '-');
console.log(reqUrl)
window.location.href = reqUrl
Use urldecode function of PHP
<?php
echo urldecode('http://prexprint.com/Laminated%20Business%20Cards');
?>
You can use
<?php echo urldecode('http://prexprint.com/Laminated%20Business%20Cards'); ?>
You should have the urls encoded, for compatability issues.
"If it ain't broke, don't fix it."
Hey, I need to delete all images from a string and I just can't find the right way to do it.
Here is what I tryed, but it doesn't work:
preg_replace("/<img[^>]+\>/i", "(image) ", $content);
echo $content;
Any ideas?
Try dropping the \ in front of the >.
Edit: I just tested your regex and it works fine. This is what I used:
<?
$content = "this is something with an <img src=\"test.png\"/> in it.";
$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);
echo $content;
?>
The result is:
this is something with an (image) in it.
You need to assign the result back to $content as preg_replace does not modify the original string.
$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);
I would suggest using the strip_tags method.
Sean it works fine i've just used this code
$content = preg_replace("/<img[^>]+\>/i", " ", $content);
echo $content;
//the result it's only the plain text. It works!!!
I wanted to display the first 300 words of a news story as a preview which unfortunately meant that if a story had an image within the first 300 words then it was displayed in the list of previews which really messed with my layout. I used the above code to hide all of the images from the string taken from my database and it works wonderfully!
$news = $row_latest_news ['content'];
$news = preg_replace("/<img[^>]+\>/i", "", $news);
if (strlen($news) > 300){
echo substr($news, 0, strpos($news,' ',300)).'...';
}
else {
echo $news;
}
$this->load->helper('security');
$h=mysql_real_escape_string(strip_image_tags($comment));
If user inputs
<img src="#">
In the database table just insert character this #
Works for me
simply use the form_validation class of codeigniter:
strip_image_tags($str).
$this->load->library('form_validation');
$this->form_validation->set_rules('nombre_campo', 'label', 'strip_image_tags');
There seems to be a bug in a Wordpress PHP function that leaves whitespace in front of the title of the page generated by <?php echo wp_title(''); ?> I've been through the Wordpress docs and forums on that function without any luck.
I'm using it this way <body id="<?php echo wp_title(''); ?>"> in order to generate an HTML body tag with the id of the page title.
So what I need to do is strip that white space, so that the body tag looks like this <body id="mypage"> instead of this <body id=" mypage">
The extra white space kills the CSS I'm trying to use to highlight menu items of the active page. When I manually add a correct body tag without the white space, my CSS works.
So how would I strip the white space? Thanks, Mark
Part Two of the Epic
John, A hex dump was a good idea; it shows the white space as two "20" spaces. But all solutions that strip leading spaces and white space didn't.
And, <?php ob_start(); $title = wp_title(''); ob_end_clean(); echo $title; ?>
gives me < body id ="">
and <?php ob_start(); $title = wp_title(''); echo $title; ?>
gives me < body id =" mypage">
Puzzle. The root of the problem is that wp_title has optional page title leading characters - that look like chevrons - that are supposed to be dropped when the option is false, and they are, but white space gets dumped in.
Is there a nuclear option?
Yup, tried them both before; they still return two leading spaces... arrgg
Strip all whitespace from the left end of the title:
<?php echo ltrim(wp_title('')); ?>
Strip all whitespace from either end:
<?php echo trim(wp_title('')); ?>
Strip all spaces from the left end of the title:
<?php echo ltrim(wp_title(''), ' '); ?>
Remove the first space, even if it's not the first character:
<?php echo str_replace(' ', '', wp_title(''), 1); ?>
Strip only a single space (not newline, not tab) at the beginning:
<?php echo preg_replace('/^ /', '', wp_title('')); ?>
Strip the first character, whatever it is:
<?php echo substr(wp_title(''), 1); ?>
Update
From the Wordpress documentation on wp_title, it appears that wp_title displays the title itself unless you pass false for the second parameter, in which case it returns it. So try:
<?php echo trim(wp_title('', false)); ?>
ltrim()
ltrim($str)
Just to throw in some variety here: trim
<body id="<?=trim(wp_title('', false));?>">
Thanks for this info! I was in the same boat in that I needed to generate page ids for CSS purposes based on the page title and the above solution worked beautifully.
I ended up having an additional hurdle in that some pages have titles with embedded spaces, so I ended up coding this:
<?php echo str_replace(' ','-',trim(wp_title('',false))); ?>
add this to your functions.php
add_filter('wp_title', create_function('$a, $b','return str_replace(" $b ","",$a);'), 10, 2);
should work like a charm