In PHP replacing text between TAGS & URI information in Title Tag


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting In PHP replacing text between TAGS & URI information in Title Tag
# 1  
Old 06-23-2009
In PHP replacing text between TAGS & URI information in Title Tag

Hi,

what I am trying to do in PHP, is to replace the Title. I need some of the URL information inside aswell depending on the domain.

The title is always different so I need to store it in a variable, put the url info like described below in front of it.
Here is an example how it should look like:


Code:
<title>This is my site</title>

should be changed into:

for
Code:
www.cool.com
<title>Cool - This is my site</title>

for
Code:
www.very-cool.com
<title>Very Cool - This is my site</title>

for
Code:
www.thisis.very-cool.com
 <title>Thisis Very Cool - This is my site</title>


I tried to start with this, but it fails somehow:

Code:
$text = '<title> bahh blahh </title>';
$pattern = "/<title\b[^>]*>(.*?)<\/title>/ims";
$content = "<title>newtitle</title>";
echo preg_replace($pattern, $content, $text);

The resukted endtitle tag looks like this:
Code:
<\/title>

if I remove "\" I get an error because it is missing...

so I now work with strpos like this:

PHP Code:
$start stripos($result"<title>")+ strlen("<title>");
$length strpos($result"</title>"$start) - $start
$text substr($result$start$length); 
Is't there a more elegant way to change the title, but I need to store the content in between.

Last edited by lowmaster; 06-23-2009 at 08:35 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find files in sub dir with tag & add "." at the beginning [tag -f "Note" . | xargs -0 {} mv {} .{}]

I am trying find files in sub dir with certain tags using tag command, and add the period to the beginning. I can't use chflags hidden {} cause it doesn't add period to the beginning of the string for web purpose. So far with my knowledge, I only know mdfind or tag can be used to search files with... (6 Replies)
Discussion started by: Nexeu
6 Replies

2. UNIX for Beginners Questions & Answers

Replacing tag based on condition

Hi All, I am having a file like below. The file will having information about the records.If you see the file the file is header and data. For example it have 1 men tag and the tag id will be come after headers. The change is I want to convert All pets tag from P to X. I did a sed like below... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

3. Shell Programming and Scripting

Finding a text in files & replacing it with unique strings

Hallo Everyone. I have to admit I'm shell scripting illiterate . I need to find certain strings in several text files and replace each of the string by unique & corresponding text. I prepared a csv file with 3 columns: <filename>;<old_pattern>;<new_pattern> ... (5 Replies)
Discussion started by: gordom
5 Replies

4. Shell Programming and Scripting

Problem while replacing text between two tags

Hi, I have a file i which i want to replace {gIntegraHost} to {gTestRunHost} between <TCPSampler and curl only. I am firing below command sed '1,/<TCPSampler/!{ /curl/,/<TCPSampler/!s/{gIntegraHost}/{gTestRunHost}/; }' file1 But only on instance is getting replaced not all. Any idea how... (6 Replies)
Discussion started by: abhitanshu
6 Replies

5. Shell Programming and Scripting

Search & Replacing text within a file

Hi all! I'm a newbie and I'm writing a script which will ask a user for data to search. I will then search for this data using grep and displaying this data back to the screen for the user to see. The user will then enter new data to replace the data searched. Now how do I replace this data... (4 Replies)
Discussion started by: macastor
4 Replies

6. Shell Programming and Scripting

Replacing text between Tags, with output from a complex command

The Problem I have n files all named "conf.cfg", each of which contain a line that reads: "MyVar=XXX", where XXX can be any value. I have a second file called report.xml, that might read: <Reports> <Report>Report1</Report> <Report>Report2</Report> <Report>Report3</Report> </Reports> I... (1 Reply)
Discussion started by: Flang
1 Replies

7. Shell Programming and Scripting

searching & replacing/removing only certain HTML tags

I generally save a lot of web pages for reading offline which works out great for school. Now I have to spend a lot of time on the bus and I am looking for the best way to read some of these webpages using my Nokia 7610. I have uploaded the files to my phone, but they are deadly deadly slow to... (2 Replies)
Discussion started by: naphelge
2 Replies

8. Homework & Coursework Questions

[Scripting]Find & replace using user input then replacing text after

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: (o) Checkout an auto part: should prompt the user for the name of the auto part and borrower's name: Name:... (2 Replies)
Discussion started by: SlapnutsGT
2 Replies

9. Shell Programming and Scripting

Replacing Last occurance of &amp; from a string

Hi All, Could anyone help me out in the below requirement: I have a text(XML) file like this: - <Dim2> <Properties Name="" State="2" ShowHir="-1" ApplyFilter="-1" ExpandToLevel="1" BreakHierType="1" MaxDepth="1" SlicerSelectOptions="1" ShowLeaf="0" HasGroup="0" /> <Expanded... (1 Reply)
Discussion started by: vivekshady
1 Replies
Login or Register to Ask a Question