Solved

Stripping out some text out of piece of code

Posted on 2011-03-18
42
231 Views
Last Modified: 2012-05-11
Hi,

This piece of code:

if(isset($SEARCH)){
                        $NAVIGATION->outputPageTitle($SEARCH);
                  }else{
                        $NAVIGATION->outputPageTitle();
                  }

outputs:
<title> xxxxxxxx </title>
+
<meta name="description" content="yyyyyyyyyyyyyyyy" />

i would like to modify the code above to only output "xxxxxxx" (also without the title-tags)

is that possible in a way?

thanks
0
Comment
Question by:peps03
  • 18
  • 11
  • 9
  • +1
42 Comments
 
LVL 13

Expert Comment

by:darren-w-
Comment Utility
Try this:

<?php
$test="<title> xxxxxxxx </title>";
echo "'".trim(strip_tags($test))."'";

?>
0
 
LVL 13

Expert Comment

by:darren-w-
Comment Utility
ps: I've added the single quotes to show that the space has been removed in this example
0
 

Author Comment

by:peps03
Comment Utility
Thanks for you reaction Darren-w,

but this is the code that needs the modifying:
if(isset($SEARCH)){
                        $NAVIGATION->outputPageTitle($SEARCH);
                  }else{
                        $NAVIGATION->outputPageTitle();
                  }

(so it only outputs xxxxxxxxx)

you are modifying the output.

0
 
LVL 1

Expert Comment

by:Imaginx
Comment Utility
Can you show us the code in the method 'outputPageTitle'
0
 
LVL 1

Expert Comment

by:Imaginx
Comment Utility
Also Peps03, I think darren-w may have been just trying to show you what to change within your method - it seems like that's where you may want to make your changes.
0
 

Author Comment

by:peps03
Comment Utility
Sure.

what i'm trying is also using the title text as the site banners alt-text.

so i can't really mess around in the code below, as it creates the page title and meta description


<?

	public function outputPageTitle($search=null){
		
		switch($this->getType()){
			
			case NAVIGATION_NORMAL:
				
				$meta = "";
				
				echo "<title>";
				
				if($this->hasObject()){
					
					$oId = $this->object();
					$city = getObjectField($oId, "plaats");
					if(is_numeric($city)){
						$city = getObjectField($city, "naam");
					}
					$title = htmlentities( getObjectField($oId, "adres") .", ". $city);
					
					
							$detail = $this->activeDetail();
					switch( $detail){
						
						case DETAIL_FOTO:
							echo "Foto's van kantoorruimte ". $title ." - KantoorBalie.nl";
							break;
						
						case DETAIL_MAP;
							echo "Bekijk de ligging van kantoorruimte ". $title ." op de kaart - KantoorBalie.nl";
							break;
							
						default:
							echo "Kantoorruimte huren ". $title ."? Bij KantoorBalie.nl kunt u kantoorruimte huren ". $title;	
							break;
							
					}

				}else if($this->plaats() != ""){
					
					$pp = $this->parsedPlaats();					
					echo "Kantoorruimte huren in ". $pp ."? Bedrijfsruimte te huur in ". $pp;

				}else if($this->provincie() != ""){
					
					$r = $this->parsedProvincie();
					echo "Kantoorruimte huren in ". $r ."? Bedrijfsruimte te huur in ". $r;

				}else if($this->tag() != ""){
					
					$ids = getObjectIdByTemplateFieldValue("tag", "permanente link", $this->tag());
					if(count($ids) > 0){
						
						echo getObjectField($ids[0], "pagina titel");
						
					}else{
						echo "Informatie over '". $this->parsedTag() ."' op Kantoorbalie.nl";
					}

				}else{	
				
					$detail = $this->activeDetail();
					switch($detail){

						case DETAIL_CITIES:
							echo "Bekijk ons kantoorruimte aanbod per stad op de kaart - KantoorBalie.nl";
							break;

						case DETAIL_PROVINCES:
							echo "Bekijk ons kantoorruimte aanbod per provincie op de kaart - KantoorBalie.nl";
							break;			
							
						default:
						
							$fId = $this->getActiveContentId();						
						if( $fId != -1){						

							$s = getStructureById($fId);
							$c = getObjectCount($s, "pagina metadata");
							if($c > 0){
								$id = getObjectId($s, 0, "pagina metadata");
								$title = getObjectField($id, "titel");
								$meta = getObjectField($id, "metadata");

								if($title != ""){
									echo $title;
								}else{
									echo getStructureNameById($fId) ." - Kantoorbalie.nl";	
								}

							}else{
								echo getStructureNameById($fId) ." - Kantoorbalie.nl";						
							}
						}						
					}					
				
				}	
				echo "</title>".PHP_EOL;
				
				if($meta != ""){
					echo '<meta name="description" content="'. htmlentities($meta) .'" />'.PHP_EOL;
				}
			
				break;
			
			case NAVIGATION_SEARCH:
				echo "<title>";
				if(isset($search)){
					
					$criteria = $search->getCriteria();
					if($criteria["plaatsId"] != -1){
						
						$pp = getObjectField($criteria["plaatsId"], "naam");
						
						echo "Kantoorruimte huren in ". $pp ."? Bij KantoorBalie.nl kunt u kantoorruimte huren in ". $pp;
						
					}else if($criteria["plaats"] != ""){
						
						echo "Kantoorruimte huren in ". $criteria["plaats"] ."? Bij KantoorBalie.nl kunt u kantoorruimte huren in ". $criteria["plaats"];
					}
				}
				echo "</title>".PHP_EOL;
				
				break;
				
			case NAVIGATION_RSS:
			
				break;
		}
	}
?>

Open in new window

0
 
LVL 13

Expert Comment

by:darren-w-
Comment Utility
yes,

so in the method outputPageTitle();

the last line might say something like

return $title;

you would need to change it like:

return trim(strip_tags($title));
0
 

Author Comment

by:peps03
Comment Utility
yes, but if i change the source, the page will also lose its title tags
0
 
LVL 13

Expert Comment

by:darren-w-
Comment Utility
Humm,

you need to overload your method with a boolean switch to show or not show title,

then in the code you need initially create a variable for title, and append the text to it as opposed to echoing it.

finally you should echo the variable you have built up, with or without the title tags dependant on the boolean value

ie

if ($showtitle)
{
echo $title;}
else{
echo trim(strip_tags($title));
}
0
 
LVL 1

Expert Comment

by:Imaginx
Comment Utility
dammit darren ... beat me to it .. lol
0
 

Author Comment

by:peps03
Comment Utility
thanks Darren ;)

but excuse me for not understanding right away.  where would i have to make these changes?

i have the big piece of code that generates the <titke> and the <meta>
and the small piece that displays both
the third piece that (has to be created?) should only echo xxxxxx

thanks!
0
 
LVL 13

Expert Comment

by:darren-w-
Comment Utility
peps03,

The changes need to be done in your code, there is a lot going on in this method; its also very complex, and could it do with being broken down into further methods for ease of reading, also I would recommend that you build the meta and title tags into a string variable  and then echo their value at the end, when you have done this you can isolate the text as in my example or add the title tags at the end, and not add them in the code.

Darren
0
 

Author Comment

by:peps03
Comment Utility
oke.. wouldn't really know how to do that.

isn't it just possible to "capture" the <title> out of this code:
if(isset($SEARCH)){
                        $NAVIGATION->outputPageTitle($SEARCH);
                  }else{
                        $NAVIGATION->outputPageTitle();
                  }

and then strip of the tag?

or get the title after it is echoed to the page? and then strip it out?

i also tried removing the title tags from the long code, and echoing it between self added <title> tags in the header, but then the <meta> tag also gets stuck in the title tag..

is there an easy way of maybe splitting the title and the meta tag? so i can echo the title between the (self added) title tags in the header (and the title text in the alt of the banner)

and then the meta-tag separately below it?
0
 
LVL 1

Expert Comment

by:Imaginx
Comment Utility
maybe add another method along the lines of

echoString($string,$type=null){
   switch($type){
       case "title":
          echo "<title>".trim($string)."</title>";
          break;
       case "meta":
          echo "<meta>".trim($string)."</meta>";
          break;
       default:
          echo trim(strip_tags($title));
          break;
   }
}


Then on your echos in the current method, replace
   echo "random string data";
with:
    $this->echoString("random string data","title"); //where title can be title, meta, or nothing for no tags ..
0
 
LVL 13

Expert Comment

by:darren-w-
Comment Utility
Hi,

Once the text has been echoed to the browser there is no way to edit with php.

dependant on how you intend to use the code you could extract it from the body with JQuery

echo $("title").text())

take this value and put it into a div on the page

$("#titletext").text($("title").text())

<div id="titletext"></div>

0
 

Author Comment

by:peps03
Comment Utility
this is getting closer to what i am looking for.

can the extracted text only be put in a div, or also in the alt-text?



0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
How about setting the string value to a variable:

if(isset($SEARCH)){
                        $mytext = $NAVIGATION->outputPageTitle($SEARCH);
                  }else{
                        $mytext = $NAVIGATION->outputPageTitle();
                  }
// just checking
//echo $mytext;

$mytext = str_replace("<title>","",$mytext);
$mytext = str_replace("</title>","",$mytext);

// double checking
echo $mytext;
0
 

Author Comment

by:peps03
Comment Utility
@ Roads
it is still outputting the normal:
<title> xxx </title>
<meta yyyyyyy />
0
 
LVL 13

Expert Comment

by:darren-w-
Comment Utility
Putting it in alt text on a hyperlink with a I
Id of linkref

$('a#linkref').attr('alt',$('title').text())
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
Well, if you need to output only the "inside stuff", comment out the lines with echo title:

public function outputPageTitle($search=null){
		
		switch($this->getType()){
			
			case NAVIGATION_NORMAL:
				
				$meta = "";
				
				//echo "<title>";
				
				if($this->hasObject()){
					
					$oId = $this->object();
					$city = getObjectField($oId, "plaats");
					if(is_numeric($city)){
						$city = getObjectField($city, "naam");
					}
					$title = htmlentities( getObjectField($oId, "adres") .", ". $city);
					
					
							$detail = $this->activeDetail();
					switch( $detail){
						
						case DETAIL_FOTO:
							echo "Foto's van kantoorruimte ". $title ." - KantoorBalie.nl";
							break;
						
						case DETAIL_MAP;
							echo "Bekijk de ligging van kantoorruimte ". $title ." op de kaart - KantoorBalie.nl";
							break;
							
						default:
							echo "Kantoorruimte huren ". $title ."? Bij KantoorBalie.nl kunt u kantoorruimte huren ". $title;	
							break;
							
					}

				}else if($this->plaats() != ""){
					
					$pp = $this->parsedPlaats();					
					echo "Kantoorruimte huren in ". $pp ."? Bedrijfsruimte te huur in ". $pp;

				}else if($this->provincie() != ""){
					
					$r = $this->parsedProvincie();
					echo "Kantoorruimte huren in ". $r ."? Bedrijfsruimte te huur in ". $r;

				}else if($this->tag() != ""){
					
					$ids = getObjectIdByTemplateFieldValue("tag", "permanente link", $this->tag());
					if(count($ids) > 0){
						
						echo getObjectField($ids[0], "pagina titel");
						
					}else{
						echo "Informatie over '". $this->parsedTag() ."' op Kantoorbalie.nl";
					}

				}else{	
				
					$detail = $this->activeDetail();
					switch($detail){

						case DETAIL_CITIES:
							echo "Bekijk ons kantoorruimte aanbod per stad op de kaart - KantoorBalie.nl";
							break;

						case DETAIL_PROVINCES:
							echo "Bekijk ons kantoorruimte aanbod per provincie op de kaart - KantoorBalie.nl";
							break;			
							
						default:
						
							$fId = $this->getActiveContentId();						
						if( $fId != -1){						

							$s = getStructureById($fId);
							$c = getObjectCount($s, "pagina metadata");
							if($c > 0){
								$id = getObjectId($s, 0, "pagina metadata");
								$title = getObjectField($id, "titel");
								$meta = getObjectField($id, "metadata");

								if($title != ""){
									echo $title;
								}else{
									echo getStructureNameById($fId) ." - Kantoorbalie.nl";	
								}

							}else{
								echo getStructureNameById($fId) ." - Kantoorbalie.nl";						
							}
						}						
					}					
				
				}	
				//echo "</title>".PHP_EOL;
				
				if($meta != ""){
					echo '<meta name="description" content="'. htmlentities($meta) .'" />'.PHP_EOL;
				}
			
				break;
			
			case NAVIGATION_SEARCH:
				//echo "<title>";
				if(isset($search)){
					
					$criteria = $search->getCriteria();
					if($criteria["plaatsId"] != -1){
						
						$pp = getObjectField($criteria["plaatsId"], "naam");
						
						echo "Kantoorruimte huren in ". $pp ."? Bij KantoorBalie.nl kunt u kantoorruimte huren in ". $pp;
						
					}else if($criteria["plaats"] != ""){
						
						echo "Kantoorruimte huren in ". $criteria["plaats"] ."? Bij KantoorBalie.nl kunt u kantoorruimte huren in ". $criteria["plaats"];
					}
				}
				//echo "</title>".PHP_EOL;
				
				break;
				
			case NAVIGATION_RSS:
			
				break;
		}
	}

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
... and just use:

if(isset($SEARCH)){
                        $NAVIGATION->outputPageTitle($SEARCH);
                  }else{
                        $NAVIGATION->outputPageTitle();
                  }
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:peps03
Comment Utility
@ roads, i tried that earlier, then it outputs the meta tag in the title
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
yes, if you need to delete it, just comment out that line (100):

//      echo '<meta name="description" content="'. htmlentities($meta) .'" />'.PHP_EOL;
0
 

Author Comment

by:peps03
Comment Utility
@ darren-w-:
how will the the jQuery code look like? i can't get it working..

i do have jquery version 1.2.6 installed..
0
 

Author Comment

by:peps03
Comment Utility
@ roads, as told above, these lines are needed to generate the sites title and meta, so i cant comment things out in the "large" code because then the site wont have a title and meta.
please read the earlier posts.
0
 
LVL 13

Expert Comment

by:darren-w-
Comment Utility
<html>
   <head>
      <title>test</title>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
      <style type="text/css">
      </style>
      <script type='text/javascript'>
         $(function(){
        $('img').attr('alt',$('title').text());
         });

      </script>
   </head>
   <body>
      <img alt="" src="test.jpg">
   </body>
</html>

Open in new window

0
 
LVL 13

Expert Comment

by:darren-w-
Comment Utility
Though, it may not  be read by a screen reader, or spider
0
 

Author Comment

by:peps03
Comment Utility
darren-w-,

tried your example but the alt stays empty..
0
 
LVL 13

Expert Comment

by:darren-w-
Comment Utility
You won't see it in the source, you need To look in firebug or something similar, I can assure you that the value is set
0
 

Author Comment

by:peps03
Comment Utility
hmm oke, but i'm trying this to make a alt text that will be visible for spiders..
0
 
LVL 13

Expert Comment

by:darren-w-
Comment Utility
My php comments apply then sorry:(
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
OK, this code below is for testing. I did add mytitle inside a class, just for now it should work only for NAVIGATION_NORMAL case.

<?

    var $mytitle = "";

	public function outputPageTitle($search=null){
		
		switch($this->getType()){
			
			case NAVIGATION_NORMAL:
				
				$meta = "";
				
				echo "<title>";
				
				if($this->hasObject()){
					
					$oId = $this->object();
					$city = getObjectField($oId, "plaats");
					if(is_numeric($city)){
						$city = getObjectField($city, "naam");
					}
					$title = htmlentities( getObjectField($oId, "adres") .", ". $city);
					
					
							$detail = $this->activeDetail();
					switch( $detail){
						
						case DETAIL_FOTO:
							echo "Foto's van kantoorruimte ". $title ." - KantoorBalie.nl";
							$this->mytitle = "Foto's van kantoorruimte ". $title ." - KantoorBalie.nl";
							break;
						
						case DETAIL_MAP;
							echo "Bekijk de ligging van kantoorruimte ". $title ." op de kaart - KantoorBalie.nl";
							$this->mytitle = "Bekijk de ligging van kantoorruimte ". $title ." op de kaart - KantoorBalie.nl";
							break;
							
						default:
							echo "Kantoorruimte huren ". $title ."? Bij KantoorBalie.nl kunt u kantoorruimte huren ". $title;
							$this->mytitle = "Kantoorruimte huren ". $title ."? Bij KantoorBalie.nl kunt u kantoorruimte huren ". $title;
							break;
							
					}

				}else if($this->plaats() != ""){
					
					$pp = $this->parsedPlaats();					
					echo "Kantoorruimte huren in ". $pp ."? Bedrijfsruimte te huur in ". $pp;
					$this->mytitle = "Kantoorruimte huren in ". $pp ."? Bedrijfsruimte te huur in ". $pp;

				}else if($this->provincie() != ""){
					
					$r = $this->parsedProvincie();
					echo "Kantoorruimte huren in ". $r ."? Bedrijfsruimte te huur in ". $r;
					$this->mytitle = "Kantoorruimte huren in ". $r ."? Bedrijfsruimte te huur in ". $r;

				}else if($this->tag() != ""){
					
					$ids = getObjectIdByTemplateFieldValue("tag", "permanente link", $this->tag());
					if(count($ids) > 0){
						
						echo getObjectField($ids[0], "pagina titel");
						$this->mytitle = getObjectField($ids[0], "pagina titel");
						
					}else{
						echo "Informatie over '". $this->parsedTag() ."' op Kantoorbalie.nl";
						$this->mytitle = "Informatie over '". $this->parsedTag() ."' op Kantoorbalie.nl";
					}

				}else{	
				
					$detail = $this->activeDetail();
					switch($detail){

						case DETAIL_CITIES:
							echo "Bekijk ons kantoorruimte aanbod per stad op de kaart - KantoorBalie.nl";
							$this->mytitle = "Bekijk ons kantoorruimte aanbod per stad op de kaart - KantoorBalie.nl";
							break;

						case DETAIL_PROVINCES:
							echo "Bekijk ons kantoorruimte aanbod per provincie op de kaart - KantoorBalie.nl";
							$this->mytitle = "Bekijk ons kantoorruimte aanbod per provincie op de kaart - KantoorBalie.nl";
							break;			
							
						default:
						
							$fId = $this->getActiveContentId();						
						if( $fId != -1){						

							$s = getStructureById($fId);
							$c = getObjectCount($s, "pagina metadata");
							if($c > 0){
								$id = getObjectId($s, 0, "pagina metadata");
								$title = getObjectField($id, "titel");
								$meta = getObjectField($id, "metadata");

								if($title != ""){
									echo $title;
									$this->mytitle = $title;
								}else{
									echo getStructureNameById($fId) ." - Kantoorbalie.nl";	
									$this->mytitle = getStructureNameById($fId) ." - Kantoorbalie.nl";	
								}

							}else{
								echo getStructureNameById($fId) ." - Kantoorbalie.nl";					
								$this->mytitle = getStructureNameById($fId) ." - Kantoorbalie.nl";					
							}
						}						
					}					
				
				}	
				echo "</title>".PHP_EOL;
				
				if($meta != ""){
					echo '<meta name="description" content="'. htmlentities($meta) .'" />'.PHP_EOL;
				}
			
				break;
			
			case NAVIGATION_SEARCH:
				echo "<title>";
				if(isset($search)){
					
					$criteria = $search->getCriteria();
					if($criteria["plaatsId"] != -1){
						
						$pp = getObjectField($criteria["plaatsId"], "naam");
						
						echo "Kantoorruimte huren in ". $pp ."? Bij KantoorBalie.nl kunt u kantoorruimte huren in ". $pp;
						
					}else if($criteria["plaats"] != ""){
						
						echo "Kantoorruimte huren in ". $criteria["plaats"] ."? Bij KantoorBalie.nl kunt u kantoorruimte huren in ". $criteria["plaats"];
					}
				}
				echo "</title>".PHP_EOL;
				
				break;
				
			case NAVIGATION_RSS:
			
				break;
		}
	}
	
	public function showMyTitle(){
        return $this->mytitle;
	}
?>

Open in new window


so after the code try yours like this

if(isset($SEARCH)){
                        $NAVIGATION->outputPageTitle($SEARCH);
                  }else{
                        $NAVIGATION->outputPageTitle();
                  }
$test = showMyTitle();
echo "$test";

Open in new window

0
 

Author Comment

by:peps03
Comment Utility
Great thanks Roads_Roads,

I'll look into it as soon as i a little time!
0
 

Author Comment

by:peps03
Comment Utility
Hi Roads,

So i put this:
$test = showMyTitle();
echo "$test";

in my image's alt-tag?

and this:
if(isset($SEARCH)){
                        $NAVIGATION->outputPageTitle($SEARCH);
                  }else{
                        $NAVIGATION->outputPageTitle();
                  }

where i want the page title?

if i do so, the <title> and <meta> of the page are generated correctly, but then, when the page start loading and reaches the point of loading the image with the alt-text, the page rendering stops and the page isn't displayed any further.

thanks
0
 

Author Comment

by:peps03
Comment Utility
i get this error:

Fatal error: Call to undefined function showMyTitle() in D:\xxxxxxxxx\header.php on line 19
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
Did you include this part of the code: ?
      
public function showMyTitle(){
        return $this->mytitle;
}

Open in new window

0
 

Author Comment

by:peps03
Comment Utility
yes i did.. i copied the whole code you provided
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
Comment Utility
Use
$test = $NAVIGATION->showMyTitle();
0
 

Author Closing Comment

by:peps03
Comment Utility
Wow, works like a charm! Great!

Thank you very much for your time and help!

Kind regards!
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
Thanks.
Remember, that this works for the case of NAVIGATION_NORMAL, if there should be NAVIGATION_SEARCH, you would have to add $this->mytitle for that case after each echo as it is in NAVIGATION_NORMAL.
0
 

Author Comment

by:peps03
Comment Utility
aha, oke. thanks for noticing.

i think i can fix that myself now.

would the echoing of the text of "NAVIGATION_SEARCH" still be done with: $test = $NAVIGATION->showMyTitle(); echo $test;  ?
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
Yes.
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now