Link to home
Start Free TrialLog in
Avatar of peps03
peps03

asked on

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
Avatar of darren-w-
darren-w-
Flag of United Kingdom of Great Britain and Northern Ireland image

Try this:

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

?>
ps: I've added the single quotes to show that the space has been removed in this example
Avatar of peps03
peps03

ASKER

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.

Can you show us the code in the method 'outputPageTitle'
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.
Avatar of peps03

ASKER

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

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));
Avatar of peps03

ASKER

yes, but if i change the source, the page will also lose its title tags
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));
}
dammit darren ... beat me to it .. lol
Avatar of peps03

ASKER

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!
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
Avatar of peps03

ASKER

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?
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 ..
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>

Avatar of peps03

ASKER

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?



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;
Avatar of peps03

ASKER

@ Roads
it is still outputting the normal:
<title> xxx </title>
<meta yyyyyyy />
Putting it in alt text on a hyperlink with a I
Id of linkref

$('a#linkref').attr('alt',$('title').text())
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

... and just use:

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

ASKER

@ roads, i tried that earlier, then it outputs the meta tag in the title
yes, if you need to delete it, just comment out that line (100):

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

ASKER

@ 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..
Avatar of peps03

ASKER

@ 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.
<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

Though, it may not  be read by a screen reader, or spider
Avatar of peps03

ASKER

darren-w-,

tried your example but the alt stays empty..
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
Avatar of peps03

ASKER

hmm oke, but i'm trying this to make a alt text that will be visible for spiders..
My php comments apply then sorry:(
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

Avatar of peps03

ASKER

Great thanks Roads_Roads,

I'll look into it as soon as i a little time!
Avatar of peps03

ASKER

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
Avatar of peps03

ASKER

i get this error:

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

Open in new window

Avatar of peps03

ASKER

yes i did.. i copied the whole code you provided
ASKER CERTIFIED SOLUTION
Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peps03

ASKER

Wow, works like a charm! Great!

Thank you very much for your time and help!

Kind regards!
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.
Avatar of peps03

ASKER

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;  ?