Solved

Stripping out some text out of piece of code

Posted on 2011-03-18
42
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 18
  • 11
  • 9
  • +1
42 Comments
 
LVL 13

Expert Comment

by:darren-w-
ID: 35165475
Try this:

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

?>
0
 
LVL 13

Expert Comment

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

Author Comment

by:peps03
ID: 35165519
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

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

Expert Comment

by:Imaginx
ID: 35165572
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
ID: 35165592
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-
ID: 35165593
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
ID: 35165619
yes, but if i change the source, the page will also lose its title tags
0
 
LVL 13

Expert Comment

by:darren-w-
ID: 35165658
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
ID: 35165682
dammit darren ... beat me to it .. lol
0
 

Author Comment

by:peps03
ID: 35165739
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-
ID: 35166010
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
ID: 35166068
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
ID: 35166134
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-
ID: 35166781
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
ID: 35167014
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
ID: 35167157
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
ID: 35167247
@ Roads
it is still outputting the normal:
<title> xxx </title>
<meta yyyyyyy />
0
 
LVL 13

Expert Comment

by:darren-w-
ID: 35167260
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
ID: 35167273
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
ID: 35167293
... and just use:

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

Author Comment

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

Expert Comment

by:Lukasz Chmielewski
ID: 35167348
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
ID: 35167351
@ 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
ID: 35167376
@ 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-
ID: 35167432
<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-
ID: 35167478
Though, it may not  be read by a screen reader, or spider
0
 

Author Comment

by:peps03
ID: 35167608
darren-w-,

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

Expert Comment

by:darren-w-
ID: 35167749
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
ID: 35167796
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-
ID: 35167872
My php comments apply then sorry:(
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35171158
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
ID: 35171501
Great thanks Roads_Roads,

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

Author Comment

by:peps03
ID: 35175720
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
ID: 35175788
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
ID: 35176108
Did you include this part of the code: ?
      
public function showMyTitle(){
        return $this->mytitle;
}

Open in new window

0
 

Author Comment

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

Accepted Solution

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

Author Closing Comment

by:peps03
ID: 35176345
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
ID: 35176361
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
ID: 35176395
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
ID: 35176456
Yes.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

688 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