[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

Stripping out some text out of piece of code

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
peps03
Asked:
peps03
  • 18
  • 11
  • 9
  • +1
1 Solution
 
darren-w-Commented:
Try this:

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

?>
0
 
darren-w-Commented:
ps: I've added the single quotes to show that the space has been removed in this example
0
 
peps03Author Commented:
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
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
ImaginxCommented:
Can you show us the code in the method 'outputPageTitle'
0
 
ImaginxCommented:
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
 
peps03Author Commented:
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
 
darren-w-Commented:
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
 
peps03Author Commented:
yes, but if i change the source, the page will also lose its title tags
0
 
darren-w-Commented:
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
 
ImaginxCommented:
dammit darren ... beat me to it .. lol
0
 
peps03Author Commented:
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
 
darren-w-Commented:
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
 
peps03Author Commented:
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
 
ImaginxCommented:
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
 
darren-w-Commented:
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
 
peps03Author Commented:
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
 
Lukasz ChmielewskiCommented:
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
 
peps03Author Commented:
@ Roads
it is still outputting the normal:
<title> xxx </title>
<meta yyyyyyy />
0
 
darren-w-Commented:
Putting it in alt text on a hyperlink with a I
Id of linkref

$('a#linkref').attr('alt',$('title').text())
0
 
Lukasz ChmielewskiCommented:
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
 
Lukasz ChmielewskiCommented:
... and just use:

if(isset($SEARCH)){
                        $NAVIGATION->outputPageTitle($SEARCH);
                  }else{
                        $NAVIGATION->outputPageTitle();
                  }
0
 
peps03Author Commented:
@ roads, i tried that earlier, then it outputs the meta tag in the title
0
 
Lukasz ChmielewskiCommented:
yes, if you need to delete it, just comment out that line (100):

//      echo '<meta name="description" content="'. htmlentities($meta) .'" />'.PHP_EOL;
0
 
peps03Author Commented:
@ 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
 
peps03Author Commented:
@ 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
 
darren-w-Commented:
<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
 
darren-w-Commented:
Though, it may not  be read by a screen reader, or spider
0
 
peps03Author Commented:
darren-w-,

tried your example but the alt stays empty..
0
 
darren-w-Commented:
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
 
peps03Author Commented:
hmm oke, but i'm trying this to make a alt text that will be visible for spiders..
0
 
darren-w-Commented:
My php comments apply then sorry:(
0
 
Lukasz ChmielewskiCommented:
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
 
peps03Author Commented:
Great thanks Roads_Roads,

I'll look into it as soon as i a little time!
0
 
peps03Author Commented:
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
 
peps03Author Commented:
i get this error:

Fatal error: Call to undefined function showMyTitle() in D:\xxxxxxxxx\header.php on line 19
0
 
Lukasz ChmielewskiCommented:
Did you include this part of the code: ?
      
public function showMyTitle(){
        return $this->mytitle;
}

Open in new window

0
 
peps03Author Commented:
yes i did.. i copied the whole code you provided
0
 
Lukasz ChmielewskiCommented:
Use
$test = $NAVIGATION->showMyTitle();
0
 
peps03Author Commented:
Wow, works like a charm! Great!

Thank you very much for your time and help!

Kind regards!
0
 
Lukasz ChmielewskiCommented:
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
 
peps03Author Commented:
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
 
Lukasz ChmielewskiCommented:
Yes.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 18
  • 11
  • 9
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now